home

JScript


//blame: shardy@@differentchairs.com


/**
 * find a value in the given list that occurs an odd number of times
 */
function findOddCount(val)
{
	var ct;
	var bucket = new Array();
	for (i=0; i<val.length; i++)
	{
		ct = bucket[ val[i] ];
		var newCt = null==ct? 1 : ct+1;
		bucket[ val[i] ] = newCt;
	}
	for (key in bucket)
	{
		ct = bucket[key];
		if (1==(ct%2))
		{
			return key;
		}
	}
	return null;
}

var  a1 = [1,1,2,2,3,3,4,4,5,5,6,7,7,7,7];
var  a2 = [10,10,7,7,6,6, 2,2,3,3,4,4,5,5,6,7,7,7,7,10,10];
var  a3 = [6,6,10,10,7,7,6,6, 2,2,3,3,4,4,5,5,6,7,7,7,7,10,10];
var  a4 = [10,10,7,7, 2,2,3,3,4,4,5,5,7,7,7,7,10,10,6];
var  a5 = [6,6];
var  a6 = [1];

WScript.Echo("odd value in a1 is " + findOddCount(a1));
WScript.Echo("odd value in a2 is " + findOddCount(a2));
WScript.Echo("odd value in a3 is " + findOddCount(a3));
WScript.Echo("odd value in a4 is " + findOddCount(a4));
WScript.Echo("odd value in a5 is " + findOddCount(a5));
WScript.Echo("odd value in a6 is " + findOddCount(a6));