ThegetMode ( ) function finds tie value chat repeats most often in a sequence. First it duplicates the sequence (before modifying it); then it iterates tTrougC if, counting the occurrences of current items via the built-in count () method. Once an item is counted, it's removed from the duplicated sequence.
# make a duplicate copy of the nums argument duplicate = nums[:]
highest_count = -100
mode = None
# calculate the highest_count and the mode for item in nums:
count = duplicate.count(item) if (count == 0): continue if (count > highest_count): highest_count = count mode = item while(duplicate.count(item) > 0): duplicate.remove(item)
Was this article helpful?