Given m, sets integers containing n elements.
I have the code below that outputs an element that occurs the maximum number of times.
def find_element_which_appeared_in_max_sets(input_set):
hash_table = {}
for pair in input_set:
for i in range(0,len(pair)):
if pair[i] in hash_table:
hash_table[pair[i]] = hash_table[pair[i]] + 1
else:
hash_table[pair[i]] = 1
max_freq = 0
for elem in hash_table:
if hash_table[elem] > max_freq:
max_freq = hash_table[elem]
max_occured_elem = elem
return max_occured_elem
input_set = {(5,4),(3,2),(4,3),(8,3)}
print ""+str(find_element_which_appeared_in_max_sets(input_set))
Output:
3
Is there a more neat / elegant way to iterate through the individual elements of a set?
source
share