-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvictorhez.java
60 lines (49 loc) · 1.4 KB
/
victorhez.java
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
51
52
53
54
55
56
57
58
59
60
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* victorhez
*/
public class victorhez {
// public static void main(String[] args) {
// Scanner sc=new Scanner(System.in);
// HashMap<Integer, Integer> map
// = new HashMap<>();
// int n=sc.nextInt();
// for (int i = 0; i < n; i++)
// {
// int num;
// num=sc.nextInt();
// map.put(num,map.get(num)+1);
// }
// for (Map.Entry<Integer, Integer> entry : map.entrySet())
// {
// System.out.println(entry.getKey() + " " + entry.getValue());
// }
// }
// }
static void countFreq(int arr[], int n) {
Map<Integer, Integer> mp = new HashMap<>();
// Traverse through array elements and
// count frequencies
for (int i = 0; i < n; i++) {
if (mp.containsKey(arr[i])) {
mp.put(arr[i], mp.get(arr[i]) + 1);
} else {
mp.put(arr[i], 1);
}
}
// Traverse through map and print frequencies
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int l=sc.nextInt();
int arr[] = new int[l];
int n = arr.length;
countFreq(arr, n);
sc.close();
}
}