Skip to main content
added 339 characters in body
Source Link
GammaFunction
  • 6.9k
  • 13
  • 23

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.

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.

Zsh, 81 74 bytes

-3 bytes by testing set difference in arithmetic mode instead, -4 bytes by using local instead of typeset

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

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, 56 52 bytes

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

Try it online! Try it online!

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

Source Link
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.