Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31dc178

Browse files
Time: 110 ms (39.78%), Space: 43.6 MB (74.47%) - LeetHub
1 parent c35bd58 commit 31dc178

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var majorityElement = function(nums) {
6+
const numsCounted = {};
7+
let majorityNum, majorityNumCount = 0;
8+
9+
// Using hashmap to store number and their total count
10+
// and then get the top counted number.
11+
nums.forEach(num => {
12+
numsCounted[num] = numsCounted[num] ? ++numsCounted[num] : 1;
13+
14+
if (numsCounted[num] > majorityNumCount) {
15+
majorityNumCount = numsCounted[num];
16+
majorityNum = num;
17+
}
18+
});
19+
20+
return majorityNum;
21+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /