Majority Element LeetCode solution in JavaScript

1
var majorityElement = function (nums) {
2
// Initialize sol and cnt to store the solution and its frequency for respective iterations...
3
let sol = 0,
4
cnt = 0;
5
// For every element i in the array...
6
for (let i = 0; i < nums.length; i++) {
7
// If cnt is equal to zero, update sol as sol = i
8
if (cnt == 0) {
9
sol = nums[i];
10
cnt = 1;
11
}
12
// If i is equal to candidate, increment cnt...
13
else if (sol == nums[i]) {
14
cnt++;
15
}
16
// Otherwise, decrement cnt...
17
else {
18
cnt--;
19
}
20
}
21
// Return & print the solution...
22
return sol;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0