Skip to main content
1 of 3
GammaFunction
  • 6.9k
  • 13
  • 23

Zsh, 66 bytes

for ((;2**$#>i++;)){j=;for s
((i&1<<j++))&&echo ${(P)j}' \c'
echo}

Pretty standard nested for loop comparison, similar the C and js answers. Try it online!


Zsh, 71 bytes

for s;a=($^@'
'$^a)
for s ($a);b+=(${(j: :)${(uof)s}})
<<<${(F)${(u)b}}

Progressively builds up the cartesian product S^N, then eliminates repeated coordinates in a single element: (0 0 1 0 -> 0 1), then eliminates repeated elements: (0 1, 0 1 -> 0 1).

Here's an expanded version, with some examples:

for s in $@; do                    # 'for s in {a,b,c}' ensures we iterate 3 times
    cprod=( ${^@}$'\n'${^cprod} )  # ex: {a,b,c}{aa,ab,ac,ba,bb,bc,ca,cb,cc}
done                               # (actually, $'a\na\na' $'a\na\nb' ...)
for xyz in $cprod; do              # ex: xyz=$'a\nb\na'
    unique_elems=${(uof)xyz}       # $'a\nb\na' -(f)-> a b a -(o)-> a a b -(u)> a b
    sets+=( ${(j: :)unique_elems}  # a b -(j: :)> 'a b'
done
unique_sets=(${(u)sets})           # a 'a b' 'a b' b -(u)> a 'a b' b
<<< ${(F)unique_sets}              # a 'a b' b -(F)> $'a\na b\nb'

Try it online!

GammaFunction
  • 6.9k
  • 13
  • 23