Hashmap solutions are trivial. I tried to solve it using java 8 lambda but can't get an O(n) solution, below O(n2) one got TLE. Do you have any better idea?

```
public boolean containsNearbyDuplicate(int[] a, int k) {
if (a == null) {
return false;
}
int n = a.length;
return IntStream.range(0, n).
parallel()
.anyMatch(
i -> IntStream.range(i+1, n).anyMatch(
j -> a[i] == a[j] && j - i <= k));
}
```