O(n) space