-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountFreqencyInArray.java
More file actions
69 lines (47 loc) · 1.32 KB
/
CountFreqencyInArray.java
File metadata and controls
69 lines (47 loc) · 1.32 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tcs.practice.array;
import java.util.HashMap;
import java.util.Map.Entry;
//public class CountFreqencyInArray {
/*
public static void main(String[] args) {
int[] arr = {10, 5, 10, 15, 10, 5};
countfrequency(arr);
}
public static void countfrequency(int[] arr) {
int n = arr.length;
int visitedarr[] = new int[arr.length];
int visited = -1;
for(int i=0;i<arr.length;i++) {
int count =1;
for(int j=i+1;j<arr.length;j++) {
if(arr[i]==arr[j]) {
count++;
visitedarr[j]=visited;
}
}
if(visitedarr[i]!=visited) {
visitedarr[i]=count;
}
}
for(int i=0;i<visitedarr.length;i++) {
if(visitedarr[i]!=visited) {
System.out.println(arr[i]+"="+visitedarr[i]);
}
}
}
}*/
public class CountFreqencyInArray {
public static void main(String[] args) {
int[] arr = {10, 5, 10, 15, 10, 5};
countFrequency(arr);
}
public static void countFrequency(int []arr) {
HashMap<Integer , Integer> map = new HashMap<>();
for(int num : arr) {
map.put(num, map.getOrDefault(num, 0)+1);
}
for(int key :map.keySet()) {
System.out.println(key+"="+map.get(key));
}
}
}