Get the App
SLTechnology News&Howtos  ›  Servers  › 

How Python returns the first K most frequent elements

Shulou Source: shulou.com Published: 2022-05-31 11:02:42 10月02日 Update

This article introduces the knowledge of "how Python returns the first K most frequent elements". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic description

Topic description

Gives a non-empty array of integers and returns the first k elements that occur most frequently.

Example

[1BZ], k = 2, output [1pm 2]

If n is the size of the array, an algorithm whose time complexity is less than O (n log n) is required.

Algorithm analysis

The question requires us to output the first k elements that appear most frequently, because k can be equal to n at most, so in the worst case we need to count the number of occurrences of all numbers. So the question is divided into two parts:

Count the number of occurrences of all different numbers

Find out the number with k before the number of occurrences.

For question 1, because the number may be large, we need to count with the help of HashMap, and the time complexity is O (n). For problem 2, there are many ways: a simple method is to sort all the times quickly, and then output the first k, so the time complexity is O (n log n), which does not meet the requirements of this question. We need to optimize it.

Because we only need to return k numbers in the end, we only need to maintain a small root heap of size k all the time. When the number of new numbers is greater than the minimum number in the heap, we update the heap. The time complexity is O (n log k), which meets the requirements of the topic.

Is there any way to further optimize it? Because k is equal to n at worst, n log k is not very ideal. Then we need a different method of sorting. There is a sorting method whose complexity is only related to the size of the number that needs to be sorted, and in this case, the number that needs to be sorted is at most n (a number appears n times). The answer is bucket sort! Bucket sorting is to use an array bucket to record the number of occurrences of each number, throw the number into the corresponding number of buckets, and then enumerate each bucket from back to front, taking out the elements until k are full. The time complexity is O (n).

Finally, the time complexity of the optimal algorithm is O (n).

Reference program

That's all for "how Python returns the first K most frequent elements". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Numbers complexity complexity times sorting time elements frequency size method problem topic output situation array algorithm statistics content more this topic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple MariaDB Microsoft Linux