Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: https://leetcode.com/problems/majority-element/
- // Solution: https://leetcode.com/problems/majority-element/discuss/51611/Java-solutions-(sorting-hashmap-moore-voting-bit-manipulation).
- class Solution {
- public int majorityElement(int[] nums) {
- int count = 0;
- int majortyElement = 0;
- for (int num: nums) {
- if (count == 0)
- majortyElement = num;
- if (num != majortyElement)
- count--;
- else
- count++;
- }
- return majortyElement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement