Skip to main content
LeetCode 219, Easy. Topics: Array, Hash Table, Sliding Window. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

Given an integer array nums and an integer k, return true *if there are two distinct indices * i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.

Examples

Constraints

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

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode 250, NeetCode All.
Last modified on August 25, 2026