File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 72
72
219|[ Contains Duplicate II] ( ./0219-contains-duplicate-ii.js ) |Easy|
73
73
225|[ Implement Stack using Queues] ( ./0225-implement-stack-using-queues.js ) |Easy|
74
74
226|[ Invert Binary Tree] ( ./0226-invert-binary-tree.js ) |Easy|
75
+ 229|[ Majority Element II] ( ./0229-majority-element-ii.js ) |Medium|
75
76
231|[ Power of Two] ( ./0231-power-of-two.js ) |Easy|
76
77
232|[ Implement Queue using Stacks] ( ./0232-implement-queue-using-stacks.js ) |Easy|
77
78
234|[ Palindrome Linked List] ( ./0234-palindrome-linked-list.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 229. Majority Element II
3
+ * https://leetcode.com/problems/majority-element-ii/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
7
+ */
8
+
9
+ /**
10
+ * @param {number[] } nums
11
+ * @return {number[] }
12
+ */
13
+ var majorityElement = function ( nums ) {
14
+ const map = new Map ( ) ;
15
+ nums . forEach ( n => map . set ( n , ( map . get ( n ) || 0 ) + 1 ) ) ;
16
+
17
+ return [ ...map ]
18
+ . filter ( ( [ key , value ] ) => value > nums . length / 3 )
19
+ . map ( ( [ key ] ) => key ) ;
20
+ } ;
You can’t perform that action at this time.
0 commit comments