Skip to the code
cf.
+10 XP

#127 Repeat Click Within k Events

easy

An analytics script records the id of the element each click lands on, in order, in clicks. A double-submit guard should fire if the same element is clicked twice within k events of each other.

Write hasQuickRepeat(clicks, k) that returns true if there are two indexes i < j with clicks[i] === clicks[j] and j - i <= k, and false otherwise.

Example 1

Input: clicks = ["buy","menu","buy","help"], k = 2
Output: true

buy is clicked at indexes 0 and 2, which are 2 apart.

Example 2

Input: clicks = ["buy","menu","help","buy"], k = 2
Output: false

The two buy clicks are 3 apart.

Constraints

  • 0 <= clicks.length <= 100000
  • 0 <= k <= 100000
  • clicks[i] is a non-empty string.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

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