-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frequency of every element
50 lines (39 loc) · 1.22 KB
/
Frequency of every element
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Frequency of every element
import java.util.*;
import java.io.*;
class GFG {
public static void main(String args[])throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine().trim());
while(t-- > 0) {
int n = Integer.parseInt(in.readLine().trim());
int a[] = new int[n];
String s[] = in.readLine().trim().split(" ");
for(int i = 0; i < n; i++) {
a[i]=Integer.parseInt(s[i]);
}
Solution ob = new Solution();
int ans[] = ob.frequency(n, a);
for(int i: ans) {
out.print(i + " ");
} out.println();
}
out.close();
}
}
class Solution {
public static int[] frequency(int n,int a[]) {
int[] ans = new int[n];
for(int i = 0; i < n; i++) {
int count = 0;
for(int j = i; j < n; j++) {
if(a[j] == a[i]) {
count++;
}
}
ans[i] = count;
}
return ans;
}
}