Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Uncommon numbers of two arrays
MediaMap 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.
Ejemplos
uncommonNumbers([1, 2, 3, 4], [3, 4, 5, 6])Devuelve [ 1, 2, 5, 6 ]
uncommonNumbers([1, 1, 2], [2, 3])Devuelve [ 1, 3 ]
uncommonNumbers([1, 2], [2, 1])Devuelve []
Preguntada en JP Morgan · Fuente
Cargando el editor…
- Entrada
uncommonNumbers([ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ])Esperado[ 1, 2, 5, 6 ] - Entrada
uncommonNumbers([ 5, 6 ], [ 6, 7 ])Esperado[ 5, 7 ] - Entrada
uncommonNumbers([ 1, 2 ], [ 2, 1 ])Esperado[]
Las pruebas ocultas se ejecutan al enviar