Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function majorityElement(nums: number[]): number {
- const map = new Map<number, number>();
- for(const n of nums) {
- if (map.has(n)) {
- map.set(n, map.get(n) + 1);
- } else {
- map.set(n, 1);
- }
- }
- const min = Math.ceil(nums.length / 2);
- for(const [n, c] of map.entries()) {
- if (c >= min) return n;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement