DEV Community

loading...

Discussion on: The Happy Number Problem

Collapse
luispeerez profile image
Luis Perez Bautista

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

  if (seenSums.includes(sum)) return false; // generic infinite loop detection mechanism
Enter fullscreen mode Exit fullscreen mode

for

  if (seenSums.has(sum)) return false; // generic infinite loop detection mechanism
Enter fullscreen mode Exit fullscreen mode