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

Zsh, 81 bytes

typeset -A p
for x;a=(${a:#$x} ${x:|a})&&[ ${x:|a} ]&&((++p[$x]))
typeset p
<<<$a

Try it online!

The expansion a=(${a:#$x} ${x:|a}) puts $x in $a if it isn't there, and takes it out if it is. Then we check if $x just got removed, and increment our pair count if it was.


With a looser definition of "list", we can shave this quite a bit.

Zsh, 56 bytes

typeset -A p l
for x;((p[$x]+=1^(l[$x]^=1)))
typeset p l

Try it online!

Prints the leftover socks as all elements in an associative array with value 1, not 0.

GammaFunction
  • 6.9k
  • 13
  • 23