:orphan: Top K Frequent Elements ======================= .. highlight:: none Problem ------- https://leetcode.com/problems/top-k-frequent-elements/ Given an integer array ``nums`` and an integer ``k``, return *the* ``k`` *most frequent elements*. You may return the answer in **any order**.   **Example 1:** .. container:: example-block **Input:** nums = [1,1,1,2,2,3], k = 2 **Output:** [1,2] **Example 2:** .. container:: example-block **Input:** nums = [1], k = 1 **Output:** [1] **Example 3:** .. container:: example-block **Input:** nums = [1,2,1,2,1,2,3,1,3,2], k = 2 **Output:** [1,2]   **Constraints:** - ``1 <= nums.length <= 10``\ :sup:```5``` - ``-10``\ :sup:```4```\ ``<= nums[i] <= 10``\ :sup:```4``` - ``k`` is in the range ``[1, the number of unique elements in the array]``. - It is **guaranteed** that the answer is **unique**.   **Follow up:** Your algorithm's time complexity must be better than ``O(n log n)``, where n is the array's size. .. highlight:: python Pattern ------- Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue) Approaches ---------- .. tab-set:: .. tab-item:: Sorting **Code** .. literalinclude:: ../problems/medium/top-k-frequent-elements/top_k_frequent_elements__sorting.py :language: python :lines: 10- **Test** >>> from top_k_frequent_elements__sorting import topKFrequent >>> sorted(topKFrequent([1, 1, 1, 2, 2, 3], 2)) [1, 2] >>> topKFrequent([1], 1) [1] .. autofunction:: top_k_frequent_elements__sorting.topKFrequent