Search⌘ K
AI Features

Solution: Contains Duplicate II

Explore how to apply the sliding window pattern to solve the Contains Duplicate II problem. Learn to use sets to track elements within a limited range and detect duplicates within a distance k efficiently. This lesson helps you understand the problem constraints, implement the algorithm with O(n) time complexity, and optimize space by maintaining a window size of k.

Statement

You are given an integer array, nums, and an integer k. Determine whether two distinct indices, i and j, are in the array, such that nums[i] == nums[j] and the absolute difference between i and j is at most k. Return TRUE if such indices exist; otherwise, return FALSE.

Constraints:

  • ...