title: Daily practice (20): Numbers that appear more than half of the times in the array
categories:[Swords offer]
tags:[Daily practice]
date: 2022/02/16
Daily practice (20): Numbers that appear more than half the times in the array
There is a number in the array that occurs more than half the length of the array, please find the number.
You can assume that the array is non-empty and that a given array always has a majority of elements.
Example 1:
Input: [1, 2, 3, 2, 2, 2, 5, 4, 2]
Output: 2
limit:
1 <= array length <= 50000
Source: LeetCode
Link: https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof
Method 1: Hash table
ideas
We know that the element with the most occurrences is greater than n/2
times, so we can use a hash table to quickly count the number of occurrences of each element.
algorithm
We use a HashMap to store each element and the number of occurrences. For each key-value pair in a hash map, the key represents an element and the value represents the number of times that element appeared.
We go through the array nums with a loop and add each element in the array to the hashmap. After this, we iterate over all key-value pairs in the hashmap and return the key with the highest value. We can also use the ring method when traversing the array nums to maintain the largest value, which saves the final traversal of the hash map.
Complexity Analysis
- Time complexity: O(n), where n is the length of the array nums. We iterate over the array nums once, and for each element in nums, inserting it into the hash table takes constant time. If the maximum value is not maintained during the traversal, the hash table needs to be traversed after the traversal, because the space occupied in the hash table is O(n) (refer to the space complexity analysis below), then the traversal time will not exceed O(n). So the total time complexity is O(n).
- Space complexity: O(n). The hash table contains at most
n - n/2
key-value pairs, so the space occupied is O(n). This is because any array of length n can only contain at most n distinct values, but the question guarantees that nums must have a mode, which will occupy (at least)n/2 + 1
numbers. So there are at mostn - (n/2 +1)
different other numbers, so at mostn - n/2
different elements.
int majorityElement(vector<int>& nums) {
unordered_map<int, int> counts;
int majority = 0, cnt = 0;
for (int num : nums) {
++counts[num];
if (counts[num] > cnt) {
majority = num;
cnt = counts[num];
}
}
return majority;
}
Method 2: Sort
ideas
If all the elements in the array nums are sorted in monotonically increasing or monotonically decreasing order, then the element with subscript n/2
(subscript starting from 0) must be the mode.
algorithm
For this algorithm, we first sort the nums array, and then return the element corresponding to the index mentioned above.
Complexity Analysis
- Time complexity: O(n log n). The time complexity of sorting an array is O(n log n).
- Space complexity: O(log n). If you use the sorting algorithm that comes with the language, you need to use O(log n) stack space. If you write your own heap sort, you only need to use O(1) extra space.
int majorityElement(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums[nums.size() / 2];
}
Method 3: Randomize
ideas
Because the subscripts of the array exceeding n/2
are occupied by the mode, so we randomly select an element corresponding to the subscript and verify that there is a high probability that the mode can be found.
algorithm
Since the number corresponding to a given subscript is likely to be a mode, we randomly pick a subscript, check whether it is a mode, and return if it is, otherwise continue to randomly select.
int majorityElement(vector<int>& nums) {
while (true) {
int candidate = nums[rand() % nums.size()];
int count = 0;
for (int num : nums) {
if (num == candidate) {
++count;
}
if (count > nums.size() /2)
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。