LeetCode 347: Top K Frequent Elements
Let’s solve LeetCode problem 347: Top K Frequent Elements. The instructions are as follows: Given an integer array nums and an integer \(k\), return the \(k\) most frequent elements. You may return the answer in any order. Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 \(k\) is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. I solved this problem on NeetCode as well. The goal was to find a solution in \(O(n)\) time and space, where \(n\) is the length of nums. I’ll be showing this solution here. Even though some solutions that take \(O(n\log n)\) time are faster for LeetCode leaderboards, this solution will scale a bit better. Let’s dive in! ...