Both are viable. #1 is more amenable to debugging because you can save the array and see it all at once. #2 offers more performance because it is a single-pass strategy, with potentially O(1) storage complexity.
If you're not sure what to do, better to stick with #1.