查找数字数组出现次数大于数组长度1/2的数字

#!/usr/bin/env python

def find_num(a_array):
    if len(a_array) == 0: return None
    records = {}
    for num in a_array:
        if records.__contains__(num):
            records[num] += 1
        else:
            records[num] = 1
    print(records)

    for record in records:
        if records[record] * 2 > len(a_array):
            print("num:%s,count:%s" % (record, records[record]))


if __name__ == '__main__':
    find_num([1, 2, 2, 2])

你可能感兴趣的:(算法,Python,python,开发语言)