15

Here is way to find the element with maximum frequency in a list in Python:

a = [1,2,3,4,1,2,3,4,1,2,3,1]

print(max(set(a), key = a.count))

Comments
  • 5
    I don't python, but that's great
  • 3
    Scala is a tad bit more readable

    val ls = //list
    ls.groupBy(identity).last._2.hd
  • 3
    Nice!

    If you also wants the counts you might try collections.Counter from the standard library.
  • 6
    Now do it in assembly
  • 1
    List.count
    Py:

    > a = [1,2,2,3]
    > a.count(2) #2
    Then i would simply do something like
    Max(A.Count(Elem) for Elem in a)
    (probably in a loop to save indexes)

    But as said earlier, check Collections, there's probably something inside to do that.
Add Comment