Skip to content

Commit dfa0a58

Browse files
committed
Add solution #229
1 parent da86363 commit dfa0a58

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
7373
225|[Implement Stack using Queues](./0225-implement-stack-using-queues.js)|Easy|
7474
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
75+
229|[Majority Element II](./0229-majority-element-ii.js)|Medium|
7576
231|[Power of Two](./0231-power-of-two.js)|Easy|
7677
232|[Implement Queue using Stacks](./0232-implement-queue-using-stacks.js)|Easy|
7778
234|[Palindrome Linked List](./0234-palindrome-linked-list.js)|Easy|

solutions/0229-majority-element-ii.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

0 commit comments

Comments
 (0)