Skip to content

Commit

Permalink
probblem on hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 15, 2024
1 parent 3ddd5cd commit 410abd9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Hashing/majorityElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Hashing;
import java.util.*;

import Strings.length;
public class majorityElement {
public static void main(String[] args) {
int arr[]={1,3,2,5,1,3,1,5,1};
HashMap<Integer,Integer>map=new HashMap<>();

for(int i=0;i<arr.length;i++){
if(map.containsKey(arr[i])){
map.put(arr[i], map.get(arr[i])+1);
}else{
map.put(arr[i],1);
}

}
Set<Integer> keyset=map.keySet();
for(Integer key:keyset){
if(map.get(key)>arr.length/3){
System.out.println(key);
}
}

}
}

0 comments on commit 410abd9

Please sign in to comment.