I was defining a function that returns all the subsets (power set) of a list of integers. Example: [1,2,3] -> [[], [1], [2], [3], [1,2], [2,3], [1,3], [1,2,3]]
The code I wrote is as follows :-
func subsets(nums []int) [][]int {
out := make([][]int, 0)
out = append(out, []int{})
for _, num := range nums {
n := len(out)
for i:=0; i<n; i++ {
out = append(out, append(out[i], num))
}
}
return out
}
Which adds the next number in the list to all the previous subsets.
Output when input = [9,0,3,5,7]
Upto input length <= 4, the function runs fine. But when it is more than that, some weird behaviour occurs, like the out should be appended [9,0,3,5] but it appends [9,0,3,7] (input list [9,0,3,5,7])
What could be the reason behind this peculiar behaviour?