Find the two records that appear only once

Write codeMedium20 min

Your task

A sync job writes every record to 2 shards, so each record ID shows up exactly twice in the combined log. Then 2 writes failed partway, and their IDs show up only once.

Write single_number_iii(nums) that returns the 2 IDs that appear once.

Input and output:

  • nums: the combined log, a list of 2 to 100,000 IDs. Each ID is an integer between -2,147,483,648 and 2,147,483,647.
  • Returns a list of the 2 IDs, smaller first.

Rules:

  • Exactly 2 different IDs appear once. Every other ID appears exactly twice.
  • The IDs can come in any order.

Then, in a sentence or two: how would you find the 2 IDs with only a few variables of extra memory?

Examples

single_number_iii(nums: list[int]) -> list[int]
Example 1
Inputsingle_number_iii([1, 2, 1, 3, 2, 5])
Output[3, 5]
Example 2
Inputsingle_number_iii([4, -7, 9, 4])
Output[-7, 9]