Skip to content

Commit 2567bb8

Browse files
committed
Hashing - Frequency of elements of an array
+
1 parent 168d656 commit 2567bb8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Hashing;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
public class Q85_FrequencyOfIntegers {
9+
public static void main(String[] args) {
10+
int[] arr = new int[]{1,2,2,3,3,4,1,5,5,6};
11+
Map<Integer, Integer> freqMap = new TreeMap<>(Collections.reverseOrder());
12+
for(int j: arr){
13+
freqMap.put(j, freqMap.getOrDefault(j, 0) + 1);
14+
}
15+
String[] ans = new String[5];
16+
for(Map.Entry<Integer, Integer> e: freqMap.entrySet())
17+
// for(int j: arr){
18+
// if(freqMap.containsKey(j)){
19+
// freqMap.put(j, freqMap.get(j) + 1);
20+
// }else{
21+
// freqMap.put(j, 1);
22+
// }
23+
// }
24+
25+
System.out.println(freqMap);
26+
}
27+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy