Mode (most common element) of a list
Python 2 - 18
max(d,key=d.count)
Since your python answer doesn't seem to print, I expect this is what you want.
Add 6 bytes for print
normally.
Matlab/Octave, 7 5 bytes
Unsurprisingly there's a built-in function for finding modes. As an anonymous function:
@mode
This returns the most commonly occuring element in the input vector with ties going to the smaller value.
Saved 2 bytes thanks to Dennis!
Pyth - 6
eo/QNQ
Try it online.
Expects input on stdin like [4,3,1,0,6,1,6,4,4,0,3,1,7,7,3,4,1,1,2,8]
. Ties are resolved by last occurrence because Python performs stable sorts.
Sorts the list by count the value in the list, then prints the last number of the list.
Q
could be replaced with d
if you initialized d
to contain the value before e.g. =d[4 3 1 0 6 4 4 0 1 7 7 3 4 1 1 2 8)
Python-esque pseudo-code:
Q=eval(input());print(sorted(Q,key=Q.count)[-1])
Full Explanation:
: Q=eval(input()) (implicit)
e : ... [-1]
o Q : orderby(lambda N: ...,Q)
/QN : count(Q,N)
Pyth's orderby
runs exactly like Python's sorted
with orderby
's first argument being the key
argument.