OneSome quick remarkremarks:
if (N === 0) return true;
can be replaced by
if (N <= 1) return true;
because in a non-empty array each element is a single "consecutive number." This saves the sorting of the array in the case \$ N = 1 \$.
Then it suffices to check the count again only if it has been incremented.
The comparison with the previous number (or null
) can be simplified slightly.
for (const num of sortedArray) {
if (prev === num - 1) {
count++;
if (count === N) return true;
} else {
count = 1;
}
prev = num;
}
One quick remark:
if (N === 0) return true;
can be replaced by
if (N <= 1) return true;
because in a non-empty array each element is a single "consecutive number." This saves the sorting of the array in the case \$ N = 1 \$.
Some quick remarks:
if (N === 0) return true;
can be replaced by
if (N <= 1) return true;
because in a non-empty array each element is a single "consecutive number." This saves the sorting of the array in the case \$ N = 1 \$.
Then it suffices to check the count again only if it has been incremented.
The comparison with the previous number (or null
) can be simplified slightly.
for (const num of sortedArray) {
if (prev === num - 1) {
count++;
if (count === N) return true;
} else {
count = 1;
}
prev = num;
}
(Inadvertently hit One quick remark:
if (N === 0) return true;
can be replaced by
if (N <= 1) return true;
because in a non-empty array each element is a single "consecutive number." This saves the post buttonsorting of the array in the case :)\$ N = 1 \$.
(Inadvertently hit the post button :)
One quick remark:
if (N === 0) return true;
can be replaced by
if (N <= 1) return true;
because in a non-empty array each element is a single "consecutive number." This saves the sorting of the array in the case \$ N = 1 \$.