@@ -0,0 +1,897 @@
+// Solution runs in O(n) time and uses O(1) space.%0A//%0A// The idea is realising that if we count majority elements, it is always greater than the%0A// half of the elements in the array. So what we do is counting repeated elements, and subtracting%0A// when new element is encountered (if after subtraction cnt is 0 - reset to the new element):%0A// arr: %5B1, 2, 3, 3, 4, 3, 5, 3, 3%5D%0A// maj: %5B1, 2, 3, 3, 3, 3, 3, 3, 3%5D%0A// cnt: %5B1, 1, 1, 2, 1, 2, 1, 2, 3%5D%0A//%0A// Alternative solutions include using hash table (O(n) time) or sorting elements in the%0A// array (O(n log n) time).%0A//%0Aclass Solution %7B%0A  public int majorityElement(int%5B%5D nums) %7B%0A    int cnt = 1, elem = nums%5B0%5D;%0A    for (int i = 1; i %3C nums.length; i++) %7B%0A      if (elem == nums%5Bi%5D) %7B%0A        cnt++;%0A      %7D else %7B%0A        cnt--;%0A        if (cnt == 0) %7B%0A          cnt = 1;%0A          elem = nums%5Bi%5D;%0A        %7D%0A      %7D%0A    %7D%0A    return elem;%0A  %7D%0A%7D%0A

add solution