Reference: LeetCode
Difficulty: Easy

## Problem

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

Example:

## Analysis

### Hash Map

Go through some cases that have duplicates, e.g. [1,0,1,1].

Time: $O(\min(N, k))$ since we don’t know how large k could be.
Space: $O(\min(N, k))$

### Sliding Window (k-size set)

We can just use a hash set of size k. Whenever there is one more element, we remove the oldest element.

Time: $O(\min(N, k))$
Space: $O(\min(N, k))$

Comment
Junhao Wang
Hi, I was a master student at USC studying Computer Science.