home

Python


#!/usr/bin/python
#
# find a value in the given list that occurs an odd number of times
#
def findOddCount(val, defVal="*"):
    bucket = {}
    for i in val:
        if bucket.has_key(i):
            bucket[i]+=1
        else:
            bucket[i] = 1
    for key in bucket.keys():
        if 1==bucket[key]%2:
            return key
    return defVal;

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]

print "odd value in a1 is %s" %    findOddCount(a1)
print "odd value in a2 is %s" %    findOddCount(a2)
print "odd value in a3 is %s" %    findOddCount(a3)
print "odd value in a4 is %s" %    findOddCount(a4)
print "odd value in a5 is %s" %    findOddCount(a5)
print "odd value in a6 is %s" %    findOddCount(a6)