From 1ab867ab43ecd85bfcce790a5889704e2ae45ab0 Mon Sep 17 00:00:00 2001 From: Anurag04-03 <143274388+Anurag04-03@users.noreply.github.com> Date: Fri, 10 Oct 2025 12:17:09 +0530 Subject: [PATCH] Create top_k_frequent.py --- top_k_frequent.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 top_k_frequent.py diff --git a/top_k_frequent.py b/top_k_frequent.py new file mode 100644 index 0000000..4f62d3f --- /dev/null +++ b/top_k_frequent.py @@ -0,0 +1,10 @@ +from collections import Counter +import heapq + +def top_k_frequent(nums, k): + freq = Counter(nums) + return [item for item, _ in heapq.nlargest(k, freq.items(), key=lambda x: x[1])] + +nums = [1, 1, 2, 2, 3] +k = 2 +print(top_k_frequent(nums, k))