Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Duplicate numbers in an array
MediaMap and Set10 min
Write a function findDuplicates(numbers) that returns the duplicate numbers of an array: the numbers that occur more than once.
numbersis an array of integers. They can be positive, negative or0.- Return a new array of numbers.
- Every duplicate number is in the result once, even if it occurs three or more times.
- Order: by the first occurrence of the number in
numbers.[1, 2, 3, 2, 1, 5]gives[1, 2], not[2, 1]. Do not sort the result. - If there are no duplicates (also for an empty array), return an empty array.
- Do not change the input array.
A set or a map helps you remember which numbers you have already seen.
Ejemplos
findDuplicates([1, 2, 3, 2, 1, 5])Devuelve [ 1, 2 ]
findDuplicates([4, 4, 4])Devuelve [ 4 ]
findDuplicates([7, 8, 9])Devuelve []
Preguntada en Cognizant · Fuente
Cargando el editor…
- Entrada
findDuplicates([ 1, 2, 3, 2, 1, 5 ])Esperado[ 1, 2 ] - Entrada
findDuplicates([ 5, 6, 5 ])Esperado[ 5 ] - Entrada
findDuplicates([ 7, 8, 9 ])Esperado[]
Las pruebas ocultas se ejecutan al enviar