forked from ossamamehmood/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Top K Frequent Elements.rtf
25 lines (25 loc) · 2.76 KB
/
Top K Frequent Elements.rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 10.0.19041}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang9 Write Top_K_Frequent element in C++ (with explanation and 8 different methods)\par
\par
\par
\par
There are many ways to find the top K frequent elements in a given array. Some of the most popular methods are as follows:\par
\par
1) Use a min-heap of size K. Iterate through the array and keep track of the frequency of each element. For each element, if its frequency is greater than the top element in the heap, remove the top element from the heap and insert the current element. At the end, the heap will contain the K most frequent elements.\par
\par
2) Use a hash map to keep track of the frequency of each element. Iterate through the array and for each element, increment its frequency in the hash map. Then, create an array of size K and fill it with the K most frequent elements from the hash map.\par
\par
3) Use a bucket sort. Iterate through the array and keep track of the frequency of each element. Then, create K buckets, each with a range of frequencies. For each element in the array, add it to the bucket with its corresponding frequency. Finally, iterate through the buckets and add the elements in each bucket to an array, starting with the bucket with the most elements.\par
\par
4) Use a quick select algorithm. Iterate through the array and keep track of the frequency of each element. Then, select the Kth element in the array (in terms of frequency). All elements before this element will be the top K frequent elements.\par
\par
5) Use a max-heap of size K. Iterate through the array and keep track of the frequency of each element. For each element, if its frequency is greater than or equal to the top element in the heap, insert it into the heap. At the end, the heap will contain the K most frequent elements.\par
\par
6) Use a Trie. Iterate through the array and for each element, insert it into a Trie. Then, traverse the Trie and keep track of the frequency of each element. Finally, create an array of size K and fill it with the K most frequent elements from the Trie.\par
\par
7) Use a Radix Tree. Iterate through the array and for each element, insert it into a Radix Tree. Then, traverse the Radix Tree and keep track of the frequency of each element. Finally, create an array of size K and fill it with the K most frequent elements from the Radix Tree.\par
\par
8) Use a Binary Search Tree. Iterate through the array and for each element, insert it into a Binary Search Tree. Then, traverse the Binary Search Tree and keep track of the frequency of each element. Finally, create an array of size K and fill it with the K most frequent elements from the Binary Search Tree.\par
}