@@ -0,0 +1,822 @@
+package com.algorithm;%0A%0Aimport java.util.HashMap;%0A%0A/**%0A * Created by JinRuoyu on 01/09/2017.%0A */%0Apublic class Majority_Element %7B%0A    public static void main(String%5B%5D main) %7B%0A        int nums%5B%5D = %7B2,1,3,3,3%7D;%0A        System.out.println(majorityElement(nums));%0A    %7D%0A%0A    public static  int majorityElement(int%5B%5D nums) %7B%0A        int num = nums%5B0%5D;%0A        HashMap%3CInteger, Integer%3E num_map = new HashMap%3CInteger, Integer%3E();%0A        if(nums.length == 1) return nums%5B0%5D;%0A        for (int n : nums%0A                ) %7B%0A            if (num_map.containsKey(n)) %7B%0A                int count = num_map.get(n)+1;%0A                num_map.put(n, count);%0A                if(count %3E nums.length/2) %7B%0A                    return n;%0A                %7D%0A            %7D%0A            else num_map.put(n, 1);%0A        %7D%0A        return num;%0A%0A    %7D%0A%7D%0A

use hashmap to find majority element of an array o(n)