https://leetcode.com/problems/subsets-ii/

Here is a recursive solution of swift. Code is really simple now.

class Solution {
    func recursive(_ arr: [Int]) -> Set<Array<Int>> {
        let l = arr.count
        guard l > 1 else { return [arr] }
        let r = recursive(Array(arr[1..<l]))
        return r.union(r.map{ [arr[0]] + $0 }).union(Set(arr.map{ [$0] }))
    }

    func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
        if nums.count > 0 {
            return Array(recursive(nums.sorted()).union(Set([[]])))
        } else {
            return [[]]
        }
    }
}

Runtime: 20 ms, faster than 84.75% of Swift online submissions for Subsets II.
Memory Usage: 19.6 MB, less than 25.00% of Swift online submissions for Subsets II.