393. Mixed-Up Ledger

A small shop numbers its receipts 1 through n and files them in a ledger that has exactly n slots. During a hurried stocktake one receipt got filed twice, and because of that another receipt number never made it into the ledger at all.

Given the array nums of length n holding the ledger contents (every value is between 1 and n), return a two-element array [repeated, missing]: the receipt number that appears twice, followed by the receipt number that does not appear anywhere. Exactly one number is repeated (exactly twice) and exactly one number is absent. The position of values inside nums carries no meaning.

Example 1

Input:
nums = [4,1,2,2]
Output:
[2,3]
Explanation:

Receipt 2 is filed twice and receipt 3 never appears, so the answer is [2, 3].

Example 2

Input:
nums = [3,1,3]
Output:
[3,2]
Explanation:

Receipt 3 appears twice and receipt 2 is absent, giving [3, 2].

Constraints

2 ≤ n = nums.length ≤ 105

1 ≤ nums[i] ≤ n

Exactly one value appears twice and exactly one value in 1..n is missing; every other value appears once.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…