Skip to content

Instantly share code, notes, and snippets.

@Maggie199
Created January 17, 2015 17:04
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 Maggie199/5e4bf2573b3e08dd7102 to your computer and use it in GitHub Desktop.
Save Maggie199/5e4bf2573b3e08dd7102 to your computer and use it in GitHub Desktop.
Leetcode #169
import java.util.Hashtable;
public class Solution {
public int majorityElement(int[] num) {
Hashtable<Integer,Integer> count = new Hashtable<Integer, Integer>();
int len = num.length;
int curr;
for(int i=0; i<len; i++){
curr = num[i];
if(count.containsKey(curr)){
int temp = count.get(curr);
if(temp+1>len/2)
return curr;
else
count.put(curr,temp+1);
} else if(1 >len/2)
return curr;
else count.put(curr,1);
}
return Integer.MAX_VALUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment