We're a place where coders share, stay up-to-date and grow their careers.
You could use Set instead of an array for seenSums, that way your validation to check the infinite loop could be done in constant time by changing
Set
seenSums
if (seenSums.includes(sum)) return false; // generic infinite loop detection mechanism
for
if (seenSums.has(sum)) return false; // generic infinite loop detection mechanism
You could use
Set
instead of an array forseenSums
, that way your validation to check the infinite loop could be done in constant time by changingfor