Top K Frequent Elements in Array

Day 11 of being consistent with DSA Today’s problem: 𝗧𝗼𝗽 𝗞 𝗙𝗿𝗲𝗾𝘂𝗲𝗻𝘁 𝗘𝗹𝗲𝗺𝗲𝗻𝘁𝘀 The goal is to return the k most frequent elements from an array. I first solved it using a hashmap + heap, which works with 𝗢(𝗻 𝗹𝗼𝗴 𝗻) 𝘁𝗶𝗺𝗲 𝗰𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆. Then I explored the optimal approach using bucket sort, where elements are grouped by their frequency. 𝗛𝗲𝗮𝗽 -> 𝗢(𝗻 𝗹𝗼𝗴 𝗻) 𝘁𝗶𝗺𝗲, 𝗢(𝗻) 𝘀𝗽𝗮𝗰𝗲 𝗕𝘂𝗰𝗸𝗲𝘁 𝘀𝗼𝗿𝘁 -> 𝗢(𝗻) 𝘁𝗶𝗺𝗲, 𝗢(𝗻) 𝘀𝗽𝗮𝗰𝗲 One thing I learned:- Sometimes grouping data based on properties (like frequency) can be more efficient than traditional sorting. Link:-https://lnkd.in/edWTqBaa GitHub Link:- https://lnkd.in/eEGnvF3h #SoftwareEngineering #DSA #ProblemSolving #LearningInPublic #NeetCode250

  • text

Good comparison between heap and bucket sort. The O(n) optimization is the best. Counting frequency with this approach definitely helps instead of using O(n log n).

To view or add a comment, sign in

Explore content categories