Nice solution! I like the early return, that way the solution doesn't double up on the numbers.
However, I think that indexOf actually runs in O(n) time, because it has to search through the array for the specified number. That means your solution is still O(n^2), because it has two nested loops.
I think the most optimized would be a mix of both, so:
Thanks for your reply! Since it was more of a conceptual answer, I didn't run it before commenting. I have now; I figured out the error and fixed it.
For those interested, the issue was that a return statement in a forEach loop actually just returns inside the loop, and doesn't return in the function. To fix it, I converted it to a plain for loop.
As for the 5 people who liked it, I'm sure they appreciated the concept behind the comment and could see past any logic errors. I don't think that quite makes them fools.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Nice solution! I like the early return, that way the solution doesn't double up on the numbers.
However, I think that
indexOf
actually runs inO(n)
time, because it has to search through the array for the specified number. That means your solution is stillO(n^2)
, because it has two nested loops.I think the most optimized would be a mix of both, so:
Thanks for your reply! Since it was more of a conceptual answer, I didn't run it before commenting. I have now; I figured out the error and fixed it.
For those interested, the issue was that a
return
statement in aforEach
loop actually just returns inside the loop, and doesn't return in the function. To fix it, I converted it to a plainfor
loop.As for the 5 people who liked it, I'm sure they appreciated the concept behind the comment and could see past any logic errors. I don't think that quite makes them fools.