Zsh, 81 bytes
typeset -A p
for x;a=(${a:#$x} ${x:|a})&&[ ${x:|a} ]&&((++p[$x]))
typeset p
<<<$a
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
Prints the leftover socks as all elements in an associative array with value 1
, not 0
.