I'm trying to push subsets into an array:
def subsets(arr)
subsets = [[]]
temp_arr = []
i = 0
while i < arr.length
temp_arr << arr[i]
subsets << temp_arr
p subsets
i+=1
end
return subsets
end
My results look like this:
[[], ["a"]]
[[], ["a", "b"], ["a", "b"]]
[[], ["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]
Why is it that every time I push the temp_array to the subset array, the previous result of pushing temp_array also changes?
Is there a way I can push the unique instances of temp_array and keep it in that state?
Also, can anyone give me a hint as to how I can get all subsets from an array?