Skip to content

Instantly share code, notes, and snippets.

@vedantb
Created May 6, 2020 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vedantb/e4c2301c537c17e9b9851aa091cf353e to your computer and use it in GitHub Desktop.
Save vedantb/e4c2301c537c17e9b9851aa091cf353e to your computer and use it in GitHub Desktop.
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
let count = 0;
let candidate;
nums.forEach((num) => {
if(count === 0) candidate = num;
count += (num === candidate) ? 1 : -1;
});
return candidate;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment