Given an integer array nums of length n where every value is in the range [0, n - 1], return [missing, duplicates].
-
missing
contains every value in the range
[0, n - 1]
that does not appear in
nums
.
-
duplicates
contains every value that appears at least twice in
nums
, listed once per distinct value.
Each of the two returned arrays must be sorted in ascending order, and missing must be the first inner array. If every value appears exactly once, return [[], []].
Function Signature
def find_missing_and_duplicates(nums: list[int]) -> list[list[int]]:
Rules
-
The result always has exactly two inner lists:
missing
first, then
duplicates
. Either one can be empty.
-
Both inner lists are sorted ascending.
-
A value that appears three or more times still appears only once in
duplicates
.
Constraints
-
1 <= n == len(nums) <= 10^5
-
0 <= nums[i] <= n - 1
Examples
Input: nums = [0,2,2,4,4]
Output: [[1,3],[2,4]]
The range is [0, 4]. Values 1 and 3 never appear, so missing = [1,3]. Values 2 and 4 each appear twice, so duplicates = [2,4].
Input: nums = [3,0,1,2]
Output: [[],[]]
nums is a permutation of [0, 3], so every value appears exactly once and both lists are empty.
Input: nums = [0,0,0]
Output: [[1,2],[0]]
The range is [0, 2]. Value 0 appears three times but is listed once in duplicates, and 1 and 2 are missing.