Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Remove duplicates from an array
MediaArrays10 min
Write a function removeDuplicates(values) that returns a new array without duplicates: every value stays once.
valuesis an array of numbers and strings. One array can hold both.- Keep the first occurrence of every value, in the original order:
[3, 1, 3, 2, 1]gives[3, 1, 2]. Do not sort the result. - Two values are the same only when they are strictly equal (
===):1and'1'are different,'a'and'A'are different. - An empty array gives
[]. - Return a new array, also when there are no duplicates. Do not change (mutate) the input array.
The interviewer asked you not to use Map or Set. A hidden test checks this rule: there new Map() and new Set() throw an error. The array method includes can tell you if a value is already in the result.
Ejemplos
removeDuplicates([3, 1, 3, 2, 1])Devuelve [ 3, 1, 2 ]
removeDuplicates(['b', 'a', 'b'])Devuelve [ 'b', 'a' ]
removeDuplicates([1, '1', 1])Devuelve [ 1, '1' ]
Preguntada en GlobalLogic · Fuente
Cargando el editor…
- Entrada
removeDuplicates([ 3, 1, 3, 2, 1 ])Esperado[ 3, 1, 2 ] - Entrada
removeDuplicates([ 'b', 'a', 'b' ])Esperado[ 'b', 'a' ] - Entrada
removeDuplicates([ 4, 5, 6 ])Esperado[ 4, 5, 6 ]
Las pruebas ocultas se ejecutan al enviar