Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar
Uncommon numbers of two arrays
MédiaMap 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.
Exemplos
uncommonNumbers([1, 2, 3, 4], [3, 4, 5, 6])Retorna [ 1, 2, 5, 6 ]
uncommonNumbers([1, 1, 2], [2, 3])Retorna [ 1, 3 ]
uncommonNumbers([1, 2], [2, 1])Retorna []
Cobrada em JP Morgan · Fonte
Carregando o 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[]
Os testes ocultos rodam no envio