mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-16 07:00:53 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.4 KiB
1.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821ebee237de8297eaee796 | JavaScript Challenge 20: Array Duplicates | 28 | javascript-challenge-20 |
--description--
Given an array of integers, return an array of integers that appear more than once in the initial array, sorted in ascending order. If no values appear more than once, return an empty array.
- Only include one instance of each value in the returned array.
--hints--
findDuplicates([1, 2, 3, 4, 5]) should return [].
assert.deepEqual(findDuplicates([1, 2, 3, 4, 5]), []);
findDuplicates([1, 2, 3, 4, 1, 2]) should return [1, 2].
assert.deepEqual(findDuplicates([1, 2, 3, 4, 1, 2]), [1, 2]);
findDuplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4]) should return [-6, 0, 2, 4, 5, 23].
assert.deepEqual(findDuplicates([2, 34, 0, 1, -6, 23, 5, 3, 2, 5, 67, -6, 23, 2, 43, 2, 12, 0, 2, 4, 4]), [-6, 0, 2, 4, 5, 23]);
--seed--
--seed-contents--
function findDuplicates(arr) {
return arr;
}
--solutions--
function findDuplicates(arr) {
const duplicates = [];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
duplicates.push(arr[i]);
}
}
}
return duplicates.sort((a, b) => a - b);
}