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.
aandbare 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
ain the order of their first occurrence ina, then the numbers frombin 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...
- Input
uncommonNumbers([ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ])Expected[ 1, 2, 5, 6 ] - Input
uncommonNumbers([ 5, 6 ], [ 6, 7 ])Expected[ 5, 7 ] - Input
uncommonNumbers([ 1, 2 ], [ 2, 1 ])Expected[]
Hidden tests run on Submit