Contains Duplicate II

Given an integer array *nums* and an integer *k*, return `true` if there are two **distinct indices** *i* and *j* such that *nums[i] == nums[j]* and *|i - j| <= k*, and return `false` otherwise.

Input Format

nums = [array of integers], k = integer

Output Format

boolean (true/false)

Constraints

  • 1 <= nums.length <= 10^5; 0 <= k <= 10^5; -10^9 <= nums[i] <= 10^9

Examples

Example 1:

Input:

nums = [1,2,3,1]
k = 3

Output:

true

Explanation:

nums[0] == nums[3] = 1 and |0-3|=3 <= k.

Example 2:

Input:

nums = [1,0,1,1]
k = 1

Output:

true

Explanation:

nums[2] == nums[3] = 1 and |2-3|=1 <= k.

Example 3:

Input:

nums = [1,2,3,1,2,3]
k = 2

Output:

false

Explanation:

No duplicates within distance k=2.

Loading...
Contains Duplicate II - Hashing DSA Problem