Created
May 6, 2020 09:07
-
-
Save vedantb/e4c2301c537c17e9b9851aa091cf353e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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