I specifically want to use Transform and Conquer for a Python mode function, and have written the version below, which I believe is basically correct (please correct me if I'm wrong). However, when there are multiple modes, only one is given, which seems technically incorrect to me, as this value is "a" mode rather then "the" mode.
How would you recommend modifying the function to resolve this problem?
def mode_presort(arr):
arr.sort() # Array must be sorted before we apply the algorithm.
i = 1
mode_frequency = 0
while i < len(arr):
run_length = 1
run_value = arr[i]
while i + run_length < len(arr) and arr[i + run_length] == run_value:
run_length += 1
if run_length > mode_frequency:
mode_frequency = run_length
mode_value = run_value
i += run_length
return mode_value
arr = [1, 1, 1, 2, 2]
print(mode_presort(arr)) # Output: 1
arr = [1, 1, 2, 2]
print(mode_presort(arr)) # Output: 2
Any help much appreciated.