Zsh, 8181 74 bytes
-3 bytes by testing set difference in arithmetic mode instead, -4 bytes by using local
instead of typeset
typesetlocal -A p
for x;a=(${a:#$x} ${x:|a})&&[ &&((${x#x:|a} ]&&((++p[$x]&&++p[$x]))
typesetlocal p
<<<$a
Try it online!Try it online!
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, 5656 52 bytes
typesetlocal -A p l
for x;((p[$x]+=1^(l[$x]^=1)))
typesetlocal p l
Try it online!Try it online!
Try it online!
Prints the leftover socks as all elements in an associative array with value 1
, not 0
.