Advertisement
sweet1cris

Untitled

Jan 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param nums: a list of integers
  4.      * @return: find a  majority number
  5.      */
  6.     public int majorityNumber(ArrayList<Integer> nums) {
  7.         int count = 0, candidate = -1;
  8.         for (int i = 0; i < nums.size(); i++) {
  9.             if (count == 0) {
  10.                 candidate = nums.get(i);
  11.                 count = 1;
  12.             } else if (candidate == nums.get(i)) {
  13.                 count++;
  14.             } else {
  15.                 count--;
  16.             }
  17.         }
  18.         return candidate;
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement