From 6dfdf997fa55f8c6b521b5ef46562de46a577bfc Mon Sep 17 00:00:00 2001 From: heesun-task Date: Fri, 13 Nov 2026 04:48:45 +0000 Subject: [PATCH 1/2] 217 contains duplicate solution in Java --- contains-duplicate/heesun-task.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 contains-duplicate/heesun-task.java diff --git a/contains-duplicate/heesun-task.java b/contains-duplicate/heesun-task.java new file mode 100644 index 0000000000..469ed546be --- /dev/null +++ b/contains-duplicate/heesun-task.java @@ -0,0 +1,18 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + // Create a set to track seen numbers + Set seen = new HashSet<>(); + + // Loop through the array and return true if a duplicate is found + for (int num : nums) { + if (!seen.add(num)) { + return true; + } + } + + return false; + } +} + +// Time Complexity: O(n) on average +// Space Complexity: O(n) From ff348eaa1e60a6ad4da56a24a77da9101111790f Mon Sep 17 00:00:00 2001 From: heesun-task Date: Sat, 14 Nov 2026 02:28:02 +0000 Subject: [PATCH 2/2] 347 top k frequent elements solution in Java --- top-k-frequent-elements/heesun-task.java | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 top-k-frequent-elements/heesun-task.java diff --git a/top-k-frequent-elements/heesun-task.java b/top-k-frequent-elements/heesun-task.java new file mode 100644 index 0000000000..7c2d5d2013 --- /dev/null +++ b/top-k-frequent-elements/heesun-task.java @@ -0,0 +1,40 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + // Count each number's frequency using a hash map. + Map freq = new HashMap<>(); + for (int num : nums) { + freq.put(num, freq.getOrDefault(num, 0) + 1); + } + + // Keep the top k numbers in a min-heap ordered by frequency. + PriorityQueue heap = new PriorityQueue<>( + (a, b) -> Integer.compare(freq.get(a), freq.get(b)) + ); + + for (int num : freq.keySet()) { + if (heap.size() < k) { + heap.offer(num); + } else if (freq.get(num) > freq.get(heap.peek())) { + // Replace the least frequent number. + heap.poll(); + heap.offer(num); + } + } + + // Return the numbers in the heap. + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = heap.poll(); + } + + return result; + } +} + +// Time Complexity: O(n + u log k) +// Space Complexity: O(u + k) +// n = nums.length, u = number of unique numbers