home

PowerShell

# blame: shardy@@differentchairs.com

#
# find a value in the given list that occurs an odd number of times
#
function findOddCount ([int[]] $val)
{
	$bucket = @{}
	foreach ($k in $val)
	{
		$bucket[$k] = $bucket[$k]+1
	}
	foreach ($vkey in $bucket.Keys)
	{
		if ( 1 -eq ($bucket[$vkey] % 2))
		{
			return $vkey
		}
	}
}

$a1 = (1,1,2,2,3,3,4,4,5,5,6,7,7,7,7)
$a2 = (10,10,7,7,6,6, 2,2,3,3,4,4,5,5,6,7,7,7,7,10,10)
$a3 = (6,6,10,10,7,7,6,6, 2,2,3,3,4,4,5,5,6,7,7,7,7,10,10)
$a4 = (10,10,7,7, 2,2,3,3,4,4,5,5,7,7,7,7,10,10,6)
$a5 = (6,6)
$a6 = (1)

Write-Host -noNewLine "odd value in a1 is "; findOddCount($a1)
Write-Host -noNewLine "odd value in a2 is "; findOddCount($a2)
Write-Host -noNewLine "odd value in a3 is "; findOddCount($a3)
Write-Host -noNewLine "odd value in a4 is "; findOddCount($a4)
Write-Host -noNewLine "odd value in a5 is "; findOddCount($a5)
Write-Host -noNewLine "odd value in a6 is "; findOddCount($a6)