Day 40: Checking Duplicate Elements in Sliding Window

🚀 Day 40 / 100 | Contains Duplicate II -Intuition: -The goal is to check whether there exist two equal elements within a distance k. -Instead of checking every pair, we only check for duplicates inside a window of size k. -If a number repeats within this window, the condition |i - j| ≤ k is satisfied, then return true. -Approach: O(n) -Use a HashSet to maintain a sliding window of size k. -Traverse through the array. -If the current element already exists in the set, return true. -Add the current element to the set. -If the window size exceeds k, remove the element that goes out of range (nums[i - k]). -If traversal completes without finding duplicates, return false. -Complexity: Time Complexity: O(n) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow

  • text

To view or add a comment, sign in

Explore content categories