dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.8 KiB
1.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68af0687ef34c76c28ffa54f | Challenge 34: Missing Numbers | 28 | challenge-34 |
--description--
Given an array of integers from 1 to n, inclusive, return an array of all the missing integers between 1 and n (where n is the largest number in the given array).
- The given array may be unsorted and may contain duplicates.
- The returned array should be in ascending order.
- If no integers are missing, return an empty array.
--hints--
findMissingNumbers([1, 3, 5]) should return [2, 4].
assert.deepEqual(findMissingNumbers([1, 3, 5]), [2, 4]);
findMissingNumbers([1, 2, 3, 4, 5]) should return [].
assert.deepEqual(findMissingNumbers([1, 2, 3, 4, 5]), []);
findMissingNumbers([1, 10]) should return [2, 3, 4, 5, 6, 7, 8, 9].
assert.deepEqual(findMissingNumbers([1, 10]), [2, 3, 4, 5, 6, 7, 8, 9]);
findMissingNumbers([10, 1, 10, 1, 10, 1]) should return [2, 3, 4, 5, 6, 7, 8, 9].
assert.deepEqual(findMissingNumbers([10, 1, 10, 1, 10, 1]), [2, 3, 4, 5, 6, 7, 8, 9]);
findMissingNumbers([3, 1, 4, 1, 5, 9]) should return [2, 6, 7, 8].
assert.deepEqual(findMissingNumbers([1, 2, 3, 4, 5]), []);
findMissingNumbers([1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 6, 8, 9, 3, 2, 10, 7, 4]) should return [11].
assert.deepEqual(findMissingNumbers([1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 6, 8, 9, 3, 2, 10, 7, 4]), [11]);
--seed--
--seed-contents--
function findMissingNumbers(arr) {
return arr;
}
--solutions--
function findMissingNumbers(arr) {
if (arr.length === 0) return [];
const n = Math.max(...arr);
const set = new Set(arr);
const result = [];
for (let i = 1; i <= n; i++) {
if (!set.has(i)) {
result.push(i);
}
}
return result;
}