-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencyofElement.java
More file actions
35 lines (30 loc) · 990 Bytes
/
Copy pathfrequencyofElement.java
File metadata and controls
35 lines (30 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class frequencyofElement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[5];
System.out.println("Enter elements of an Array: ");
for(int i=0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Frequency of elements in the array: ");
for(int i=0; i < arr.length; i++) {
int count = 1;
boolean counted = false;
for(int j=0; j < i; j++) {
if(arr[i] == arr[j]) {
counted = true;
break;
}
}
if(!counted) {
for(int k=i+1; k < arr.length; k++) {
if(arr[i] == arr[k]) {
count++;
}
}
System.out.println(arr[i] + " occurs " + count + " times");
}
}
}
}