Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Remove duplicates from an array
MoyenArrays10 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.
Exemples
removeDuplicates([3, 1, 3, 2, 1])Renvoie [ 3, 1, 2 ]
removeDuplicates(['b', 'a', 'b'])Renvoie [ 'b', 'a' ]
removeDuplicates([1, '1', 1])Renvoie [ 1, '1' ]
Posé chez GlobalLogic · Source
Chargement de l'éditeur...
- Entrée
removeDuplicates([ 3, 1, 3, 2, 1 ])Attendu[ 3, 1, 2 ] - Entrée
removeDuplicates([ 'b', 'a', 'b' ])Attendu[ 'b', 'a' ] - Entrée
removeDuplicates([ 4, 5, 6 ])Attendu[ 4, 5, 6 ]
Les tests cachés se lancent à la soumission