My JAVA solution using HashMap


  • 0
    X
    public class Solution {
            public int majorityElement(int[] num) {
                Map<Integer,Integer> result = new HashMap<Integer, Integer>();
                int length = num.length;
                for (int i : num) {
                    if (result.containsKey(i)) {
                        result.put(i,result.get(i) + 1);
                    } else {
                        result.put(i,1);
                    }
                    if (result.get(i) >= length / 2 + 1)
                        return i;
                }
                return 0;
            }
        }

  • 0
    X
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.