Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active May 25, 2018 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/5aebef13a80bcb2401e252c899394621 to your computer and use it in GitHub Desktop.
Save thmain/5aebef13a80bcb2401e252c899394621 to your computer and use it in GitHub Desktop.
import java.util.*;
public class MajorityElementHashing {
public static void find(int [] arrA){
boolean found = false;
HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i <arrA.length ; i++) {
if(map.containsKey(arrA[i])){
int count = map.get(arrA[i]);
map.put(arrA[i],++count);
}else
map.put(arrA[i],1);
}
Set set = map.keySet();
Iterator<Integer> iterator = set.iterator();
while (iterator.hasNext()){
int key = iterator.next();
if(map.get(key)>arrA.length/2){
System.out.println("(Hashing)Element appearing more than n/2 times: " + key);
found = true;
break;
}
}
if(!found)
System.out.println("No element appearing more than n/2 times");
}
public static void main(String[] args) {
int [] arrA = {1,3,5,5,5,5,4,1,5};
find(arrA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment