forked from dnshi/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMajorityElement.js
More file actions
34 lines (31 loc) · 1.03 KB
/
MajorityElement.js
File metadata and controls
34 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Source : https://oj.leetcode.com/problems/majority-element/
// Author : Dean Shi
// Date : 2015-06-18
/**********************************************************************************
*
* Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
*
* You may assume that the array is non-empty and the majority element always exist in the array.
*
* Credits:Special thanks to @ts for adding this problem and creating all test cases.
*
**********************************************************************************/
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
// Moore Voting Algorithm
var majority, count = 0;
nums.forEach(function(n) {
if (count === 0) {
majority = n;
count = 1;
} else {
n === majority ? ++count : --count;
}
});
return majority;
};
// Test case
console.log(majorityElement([1, 2, 1, 1, 1, 2, 3, 1, 3, 1]) === 1);