Majority Element
You get an array in which one value fills more than half of the places. Find that value. Our example: [3, 1, 3, 2, 3, 3, 1] — the answer is 3, it appears 4 times out of 7.
- input
nums: an array of integers. One of its numbers takes more than half of its places.- returns
- That number, the majority element.
- constraints
- 1 ≤ nums.length ≤ 2000
- −10⁹ ≤ nums[i] ≤ 10⁹
- one number is there more than nums.length / 2 times
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {number}
*/
function majorityElement(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more hash map problems
- 1Two SumEasysolve
- 217Contains DuplicateEasysolve
- 242Valid AnagramEasysolve
- 383Ransom NoteEasysolve
- 205Isomorphic StringsEasysolve
- 290Word PatternEasysolve
- 219Contains Duplicate IIEasysolve
- 49Group AnagramsMediumsolve
- 128Longest Consecutive SequenceMediumsolve
- 36Valid SudokuMediumsolve
- 138Copy List with Random PointerMediumsolve
- 149Max Points on a LineHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.