Skip to content

Commit

Permalink
subsest with dups
Browse files Browse the repository at this point in the history
  • Loading branch information
GokhanCagritekin committed Jul 18, 2022
1 parent bebf563 commit cd8b031
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/backtracking/subsetsWithDup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "sort"

func subsetsWithDup(nums []int) [][]int {
n := len(nums)
ans := make([][]int, 0, 1<<n)
curr := make([]int, 0, n)
sort.Ints(nums)
var backtrack func(idx int)
backtrack = func(idx int) {
ans = append(ans, append([]int{}, curr...))
for i := idx; i < n; i++ {
if i > idx && nums[i] == nums[i-1] {
continue
}
curr = append(curr, nums[i])
//not backtrack(idx + 1)!!
backtrack(i + 1)
curr = curr[:len(curr)-1]
}
}
backtrack(0)
return ans
}

0 comments on commit cd8b031

Please sign in to comment.