Find duplicate values
beginner Arrays Challenge 17 of 18
Write findDuplicates(arr), for example to check that a page lists no user ID or product SKU twice.
Requirements
- Return an array containing each value that appears more than once in
arr, once. - Order the result by where each value first appeared in
arr. - Compare values strictly: the number
1and the string'1'are different values. - Return
[]for an empty array or when nothing repeats. - Do not mutate
arr.
Expected output
findDuplicates([1, 2, 3, 2, 4, 1]) → [1, 2] findDuplicates(['b', 'a', 'a', 'b', 'b']) → ['b', 'a'] findDuplicates([1, '1', 2]) → []
Test cases (6)
- ○returns repeated numbers in first-seen order
- ○orders by first appearance, not by second
- ○lists each duplicate only once
- ○treats numbers and strings as different values
- ○returns an empty array when nothing repeats or the input is empty
- ○does not mutate the input
solution.jsExecutes in your browser
Ctrl/⌘ + Enter to run tests · your code is saved in this browser
Results
Run the tests to see results here.