Advertisement
Guest User

169. Majority Element | Time: O(n) | Space:O(1)

a guest
Aug 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. // Problem: https://leetcode.com/problems/majority-element/
  2. // Solution: https://leetcode.com/problems/majority-element/discuss/51611/Java-solutions-(sorting-hashmap-moore-voting-bit-manipulation).
  3.  
  4. class Solution {
  5.     public int majorityElement(int[] nums) {
  6.         int count = 0;
  7.         int majortyElement = 0;
  8.         for (int num: nums) {
  9.             if (count == 0)
  10.                 majortyElement = num;
  11.             if (num != majortyElement)
  12.                 count--;
  13.             else
  14.                 count++;
  15.         }
  16.         return majortyElement;
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement