forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority-element.js
More file actions
35 lines (27 loc) · 1.07 KB
/
majority-element.js
File metadata and controls
35 lines (27 loc) · 1.07 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
// 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.
// Could you solve the problem in linear time and in O(1) space?
// Constraints:
// n == nums.length
// 1 <= n <= 5 * 10 ^ 4
// -2 ^ 31 <= nums[i] <= 2 ^ 31 - 1
const findMajorityElement = (nums) => {
if (!nums.length) throw new Error('The array is empty');
let majorityElement = 0;
// For every bit
for (let i = 0; i < 32; i++) {
const mask = 1 << i;
let bitCount = 0;
// we count the number of numbers that have this bit
for (const num of nums) {
if (num & mask) bitCount++;
}
// If bitCount is more than n / 2, we set this bit in the result
if (bitCount > nums.length / 2) majorityElement |= mask;
}
return majorityElement;
};
console.log(findMajorityElement([2, 2, 1, 1, 1, 2, 2])); // 2
console.log(findMajorityElement([7, 7, -5, 7, -5, 1, -5, 7, -5, -5, 7, 7, 7, 7, 7, 7])); // 7
console.log(findMajorityElement([4])); // 4