File tree 1 file changed +9
-7
lines changed
scripts/algorithms/M/Majority Element II
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 105 ms (Top 48.44%) | Memory: 43 MB (Top 93.77%)
2
+
1
3
/**
2
4
* @param {number[] } nums
3
5
* @return {number[] }
@@ -6,15 +8,15 @@ var majorityElement = function(nums) {
6
8
const objElement = { } ;
7
9
const timesVar = Math . floor ( nums . length / 3 ) ;
8
10
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 ;
11
+
12
+ for ( var indexI = 0 ; indexI < nums . length ; indexI ++ ) {
13
+ if ( objElement [ nums [ indexI ] ] ) {
14
+ objElement [ nums [ indexI ] ] = objElement [ nums [ indexI ] ] + 1 ;
13
15
}
14
16
else objElement [ nums [ indexI ] ] = 1 ;
15
-
17
+
16
18
if ( objElement [ nums [ indexI ] ] > timesVar ) resultSet . add ( nums [ indexI ] ) ;
17
19
}
18
-
20
+
19
21
return [ ...resultSet ] ;
20
- } ;
22
+ } ;
You can’t perform that action at this time.
0 commit comments