Solution: Majority Element
Let's solve the Majority Element problem using the Knowing What To Track pattern.
Statement#
Given an array, nums, having integers, return the majority element. An element will be considered a majority element if it occurs more than times in the array.
Note: It is safe to assume that the majority element always exists in the array.
Constraints
-
nums.length -
nums[i]
Solution#
So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
A naive approach to solve this problem is to traverse the nums array by using a nested loop. This approach traverses the array for each element to count its occurrences. An element, whose count surpasses half of the array’s length, is returned as the result.
We indeed got our desired output. However, this approach is computationally expensive, especially for large arrays, because it requires iterating over the array repeatedly, which results in a time complexity of , where is the number of elements in the nums array.
Let’s try to devise some optimized approach that costs us less than this highly computationally expensive solution.
Optimized approach using knowing what to track#
An optimized approach to solve this problem is to keep track of the occurrences of the elements using the hash map. We iterate over nums and store the count of occurrences of each element in the hash map records. After effectively storing the frequency of each number, we return the element with the highest frequency as it will be the majority element.
Let’s look at the illustration to get a better understanding of the solution.
1 of 7
2 of 7
3 of 7
4 of 7
5 of 7
6 of 7
7 of 7
Let’s implement the algorithm as discussed above:
Solution summary#
-
Create a hash map,
records, to store the frequencies of each element in thenumsarray. -
Iterate through each element in
numsand update its frequency in therecordsaccordingly. -
Return the element with the highest frequency.
Time complexity#
The time complexity of this solution is , where is the number of elements in nums. This is because the for loop takes time to store the count of occurrences of each element in the hash map, and then finding the element with the highest count also takes time. This leads to the total time complexity of , which simplifies to .
Space complexity#
In an array of length , there can be at most distinct values. However, since nums is guaranteed to have a majority element, that means that are exactly the same elements. This implies that the hash map will hold a maximum of elements, which will require space. Therefore, the space complexity of this solution is .
Can we do better?#
Another more optimized approach is Boyer-Moore’s voting algorithm. This algorithm uses two variables to keep track of the majority element as it iterates through the array. The algorithm uses the fact that the majority element, by definition, occurs more frequently than any other element to cancel out the counts of non-majority elements. By canceling out all occurrences of elements that are not the majority element, this algorithm is able to identify the majority element in a single pass through the array. It has a time complexity of and a space complexity of , making it a highly efficient solution to this problem.
Majority Element
Ransom Note