There are many numbers on the list, but how do you find the most duplicated numbers? For example, I want to know how to find the most number 6 in the list [1,1,1,1,3,4,5,5,6,6,6,6] that comes out four times.
python
There are many ways.
import itertools as it
L = [1,1,1,3,4,5,5,6,6,5,5,5,6,6]
item_cnt = {k: len(list(v)) for k, v in it.groupby(sorted(L))}
max(item_cnt.keys(), key=lambda n:item_cnt[n])
Out[46]: 5
© 2024 OneMinuteCode. All rights reserved.