-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityElement.js
More file actions
55 lines (47 loc) · 1.2 KB
/
majorityElement.js
File metadata and controls
55 lines (47 loc) · 1.2 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* LeetCode 169. Majority Element
* https://leetcode.com/problems/majority-element/description/
*
* Given an array nums of size n, return the majority element. The majority
* element is the element that appears more than ⌊n / 2⌋ times. You may assume
* that the majority element always exists in the array.
*/
/**
* @param {number[]}
* @return {number}
*/
// Boyer-Moore Voting Algorithm.
const majorityElement = (nums) => {
let candidate = null;
let count = 0;
for (const num of nums) {
// If count is 0, we need a new candidate
if (count === 0) {
candidate = num;
}
// If current number matches candidate, increment.
// Otherwise, "cancel" it out by decrementing.
count += num === candidate ? 1 : -1;
}
return candidate;
};
/*
function majorityElement(nums) {
if (nums.length === 1) {
return nums[0];
}
const map = new Map();
for (let i = 0; i < nums.length; i++) {
if (map.has(nums[i])) {
const amount = map.get(nums[i]);
if (amount === Math.floor(nums.length / 2)) {
return nums[i];
}
map.set(nums[i], amount + 1);
} else {
map.set(nums[i], 1);
}
}
}
*/
module.exports = { majorityElement };