Skip to the code
cf.
+25 XP

#237 Find the Reused Asset ID

medium

A design system's build gives each of n icons an id from 1 to n. A bug wrote n + 1 ids into the manifest ids, so at least one id appears more than once. Exactly one id value is repeated, though it may appear more than twice.

Write findReusedId(ids) that returns the repeated id. Don't change ids, and use only O(1) extra space, so sorting a copy or using a set is out.

Example 1

Input: ids = [3,1,4,4,2]
Output: 4

4 appears twice.

Example 2

Input: ids = [3,1,3,4,2]
Output: 3

3 appears twice.

Constraints

  • 2 <= ids.length <= 10^5
  • ids.length is n + 1 and 1 <= ids[i] <= n.
  • Exactly one value appears two or more times.
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).