Find every missing and every duplicated value in an array with values 0 to n-1

Quick Overview

Given an array of length n whose values lie between 0 and n - 1, return every value that never appears and every value that appears more than once, both sorted. The problem tests counting in linear time and producing an exact, ordered output.

Find every missing and every duplicated value in an array with values 0 to n-1

Company: Groww

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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]`. ```text 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. ```text 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.

Overview: Given an array of length n whose values lie between 0 and n - 1, return every value that never appears and every value that appears more than once, both sorted. The problem tests counting in linear time and producing an exact, ordered output.

Groww logo
Groww
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.