Here's part 1 JS, part 2 is a big performance issue. EDIT: I tried using new Map() for a more efficient process but I still ran into the call stack size error. I guess Javascript and heavy processing don't fit together :(
You can use a map to store the numbers you've seen. Map lookup is O(1), compared to looking through a list (O(n)). The keys can be the numbers you've seen, the values the list of positions.
Here's part 1 JS, part 2 is a big performance issue. EDIT: I tried using
new Map()
for a more efficient process but I still ran into the call stack size error. I guess Javascript and heavy processing don't fit together :(You can use a map to store the numbers you've seen. Map lookup is O(1), compared to looking through a list (O(n)). The keys can be the numbers you've seen, the values the list of positions.
Thank you for the suggestion! I'll try it!