Created
January 17, 2015 17:04
-
-
Save Maggie199/5e4bf2573b3e08dd7102 to your computer and use it in GitHub Desktop.
Leetcode #169
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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