forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-numberII.js
More file actions
38 lines (30 loc) · 1.13 KB
/
single-numberII.js
File metadata and controls
38 lines (30 loc) · 1.13 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
// Given an integer array nums where every element appears
// three times except for one, which appears exactly once.
// Find the single element and return it.
// Your algorithm should have a linear runtime complexity.
// Could you implement it without using extra memory?
// Constraints:
// 1 <= nums.length <= 3 * 10 ^ 4
// -2 ^ 31 <= nums[i] <= 2 ^ 31 - 1
// Each element in nums appears exactly three times except
// for one element which appears once.
const findSingleNumber = (nums) => {
if (!nums.length) throw new Error('The array is empty.');
let singleNumber = 0;
let mask = 1;
// We iterate over every bit
for (let i = 0; i < 32; i++) {
let count = 0;
// And we count how many numbers have this bit set to 1
for (const num of nums) {
count += num & mask;
}
// If the number of such numbers is not divisible by 3,
// it means the single number has this bit set to 1
if (count % 3) singleNumber |= mask;
mask <<= 1;
}
return singleNumber;
};
console.log(findSingleNumber([0, 1, 0, 1, 0, 1, 99])); // 99
console.log(findSingleNumber([-10, 2, 1, 6, 2, 1, 6, 2, 1, -10, 6, -8, -10])); // -8