Code Lab

Uncommon numbers of two arrays

MediumMap and Set10 min

Write a function uncommonNumbers(a, b) that returns the numbers that are in exactly one of two arrays.

  • a and b are arrays of integers, zero and negative numbers too. They can be empty and can have repeated numbers.
  • A number that is in both arrays (in their intersection) is not in the result.
  • Every number is in the result once, even if it is a duplicate inside its array: [1, 1, 2] and [2, 3] give [1, 3].
  • Order: first the numbers from a in the order of their first occurrence in a, then the numbers from b in the same way. Do not sort the result.
  • Return a new array. If there are no such numbers, return an empty array.

A set of each array helps you check quickly if a number is in it.

Examples

uncommonNumbers([1, 2, 3, 4], [3, 4, 5, 6])

Returns [ 1, 2, 5, 6 ]

uncommonNumbers([1, 1, 2], [2, 3])

Returns [ 1, 3 ]

uncommonNumbers([1, 2], [2, 1])

Returns []

Asked at JP Morgan · Source

Loading the editor...
  1. InputuncommonNumbers([ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ])
    Expected[ 1, 2, 5, 6 ]
  2. InputuncommonNumbers([ 5, 6 ], [ 6, 7 ])
    Expected[ 5, 7 ]
  3. InputuncommonNumbers([ 1, 2 ], [ 2, 1 ])
    Expected[]

Hidden tests run on Submit.