Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /**
- * @param nums: a list of integers
- * @return: find a majority number
- */
- public int majorityNumber(ArrayList<Integer> nums) {
- int count = 0, candidate = -1;
- for (int i = 0; i < nums.size(); i++) {
- if (count == 0) {
- candidate = nums.get(i);
- count = 1;
- } else if (candidate == nums.get(i)) {
- count++;
- } else {
- count--;
- }
- }
- return candidate;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement