DEV Community

Discussion on: Creating 3 Stacks With 1 Array in JavaScript

Collapse
 
voliva profile image
Víctor Oliva

Hey Emma, that's a nice solution, but wouldn't something like this work as well?

Stack 1 indices: N*3
Stack 2 indices: N*3 + 1
Stack 3 indices: N*3 + 2

So basically you split the array where position 0 is stack 1, 1 is stack 2, 2 is stack 3, 3 is stack 1 again, 4 is stack 2, etc.

This would allow your stacks to have an indefinite max length as well (as you'll never run in a case where a stack needs to take space from another one)

Collapse
 
snowfrogdev profile image
Philippe Vaillancourt • Edited

I was thinking the same thing but then you would end up with a sparse array. I guess it's not that big a deal in JS.