The problem. Given an array nums of size n, return the majority element — the value that appears more than n / 2 times. You may assume a majority element always exists.
Input: nums = [3, 2, 3]
Output: 3
Input: nums = [2, 2, 1, 1, 1, 2, 2]
Output: 2"More than half" is a strong promise: the majority value outnumbers all the other values put together. Keep that sentence in mind — the best solution is built entirely on it.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come