Contains Duplicate II

Try to solve the Contains Duplicate II problem.

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:

  • 11 \leq nums.length 103\leq 10^3

  • 103-10^3 \leq nums[i] 103\leq 10^3

  • 00 \leq k 104\leq 10^4

Examples

Press + to interact
canvasAnimation-image
1 of 3

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

1

What is the output if the following array and k = 1 are given as input?

nums = [1000, 1000]

A)

TRUE

B)

FALSE

C)

The input is invalid.

Question 1 of 40 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Try it yourself

Implement your solution in the following coding playground.

Press + to interact
Java
usercode > Solution.java
public class Solution {
public static boolean containsNearbyDuplicate(int[] nums, int k) {
// Replace this placeholder return statement with your code
return false;
}
}
Contains Duplicate II

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.