Skip to the code
+10 XP

#23 Repeat Within K Requests

easy

A form submits to the server and logs a fingerprint of each submission in ids. A double submit shows up as the same fingerprint twice, close together.

Write repeatWithinK(ids, k) that returns true if there are two different indexes i and j with ids[i] === ids[j] and Math.abs(i - j) <= k.

Example 1

Input: ids = ["a","b","c","a"], k = 3
Output: true

The two as are 3 apart, which is within k = 3.

Example 2

Input: ids = ["a","b","c","a"], k = 2
Output: false

The only repeat is 3 apart, more than 2.

Constraints

  • 0 <= ids.length <= 10^5
  • 0 <= k <= 10^5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).