Timer: 10 min, like in an interview. Hints and autocomplete are off while it runs
Remove duplicates from an array
MediumArrays10 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.
Examples
removeDuplicates([3, 1, 3, 2, 1])Returns [ 3, 1, 2 ]
removeDuplicates(['b', 'a', 'b'])Returns [ 'b', 'a' ]
removeDuplicates([1, '1', 1])Returns [ 1, '1' ]
Asked at GlobalLogic · Source
Loading the editor...
- Input
removeDuplicates([ 3, 1, 3, 2, 1 ])Expected[ 3, 1, 2 ] - Input
removeDuplicates([ 'b', 'a', 'b' ])Expected[ 'b', 'a' ] - Input
removeDuplicates([ 4, 5, 6 ])Expected[ 4, 5, 6 ]
Hidden tests run on Submit