Movatterモバイル変換


[0]ホーム

URL:


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

Commit71df0e0

Browse files
authored
Create 169-majority-element.js
Solved majority-element in JS.
1 parentc17e82c commit71df0e0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎javascript/169-majority-element.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// problem link https://leetcode.com/problems/majority-element
2+
// time complexity is O(n)
3+
4+
5+
varmajorityElement=function(nums){
6+
7+
constoccuranceOfElement=newMap();
8+
for(leti=0;i<nums.length;i++){
9+
if(occuranceOfElement.has(nums[i])){
10+
letoccurance=occuranceOfElement.get(nums[i]);
11+
occuranceOfElement.set(nums[i],occurance+1);
12+
}else{
13+
occuranceOfElement.set(nums[i],1);
14+
}
15+
}
16+
17+
for(let[key,value]ofoccuranceOfElement){
18+
if(value>nums.length/2)returnkey;
19+
}
20+
21+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp