Back to course home
0% completed
Vote For New Content
Contains Duplicate II (easy)
Problem Statement
Given an array of integers nums
and an integer k
, return true
if there are any two different indices i
and j
in the array where nums[i] == nums[j]
and abs(i - j) <= k
. Otherwise, return false
.
Examples
Example 1:
- Input:
nums = [10, 20, 10, 30], k = 1
- Output:
false
- Explanation: The number
10
appears at positions0
and2
. The difference between these positions is2
, which is not less thank
.
Example 2:
- Input:
nums = [5, 15, 25, 5, 35], k = 3
- Output:
true
- Explanation: The number
5
appears at positions0
and3
. The difference between these positions is3
, which is equal tok
.
Example 3:
- Input:
nums = [7, 8, 9, 7, 10, 11], k = 4
- Output:
true
- Explanation: The number
7
appears at positions0
and3
. The difference between these positions is3
, which is less than tok
.
Constraints:
- 1 <= nums.length <= 10<sup>5</sup>
- -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>
- 0 <= k <= 10<sup>5</sup>
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible