Skip to content

Instantly share code, notes, and snippets.

@mayonesa
Created May 8, 2017 02:32
Show Gist options
  • Save mayonesa/4fa6e4a79b1f5fb76cb0982dcfd91776 to your computer and use it in GitHub Desktop.
Save mayonesa/4fa6e4a79b1f5fb76cb0982dcfd91776 to your computer and use it in GitHub Desktop.
def mode(data):
def loop(i, counts, c_max):
max_val = c_max[0]
if (i < 0):
return max_val
else:
c_val = data[i]
new_count = counts.get(c_val, 0) + 1
counts[c_val] = new_count
max_dup = c_max[1]
return loop(i - 1, counts, (c_val, new_count) if new_count > max_dup else c_max)
return loop(len(data) - 1, dict(), (float('-inf'), 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment