Skip to main content
Code Review

Return to Answer

added 379 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

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;
 }
Post Undeleted by Martin R
added 152 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

(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 \$.

Post Deleted by Martin R
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

(Inadvertently hit the post button :)

default

AltStyle によって変換されたページ (->オリジナル) /