Majority Element II LeetCode solution in JavaScript

1
/**
2
* @param {number[]} nums
3
* @return {number[]}
4
*/
5
var majorityElement = function (nums) {
6
const objElement = {};
7
const timesVar = Math.floor(nums.length / 3);
8
const resultSet = new Set();
9

10
for (var indexI = 0; indexI < nums.length; indexI++) {
11
if (objElement[nums[indexI]]) {
12
objElement[nums[indexI]] = objElement[nums[indexI]] + 1;
13
} else objElement[nums[indexI]] = 1;
14

15
if (objElement[nums[indexI]] > timesVar) resultSet.add(nums[indexI]);
16
}
17

18
return [...resultSet];
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0