-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrequencyCount.java
More file actions
36 lines (32 loc) · 1.13 KB
/
Copy pathFrequencyCount.java
File metadata and controls
36 lines (32 loc) · 1.13 KB
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
36
import java.io.*;
public class FrequencyCount {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE RANGE=");
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
System.out.println("ENTER THE NUMBERS=");
int feq, count = 0;
int f[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(br.readLine());
}
for (int i = 0; i < n; i++) {
feq = 1; // Reset frequency for each element
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
feq++;
arr[j] = 0; // Mark repeated elements as 0 to avoid recounting
}
}
if (arr[i] != 0) {
f[count++] = feq;
}
}
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
System.out.println(arr[i] + " APPEARS FOR " + f[i] + " TIMES");
}
}
}
}