DannyPhantomGhost
DannyPhantomGhost

Reputation: 21

Number with highest frequency in a list

I am trying to write a program that will show me the number that appears most frequently in a list. I am trying to do this without importing any libraries. So far I have something that will give me the number of times the most frequent # appears but I want the number itself, not how many times it appears. How can I change what I have so far in a simple way to do this?

def freq(L):
    st = []
    L.sort()
    for i in L:
        st.append(L.count(i))
    print max(st)

EDIT: for example, freq([4, 6, 4, 3, 9, 1, 4]) is returning 3 since 4 appears 3 times. But I would like it to return 4 as in the most frequent value.

Upvotes: 2

Views: 2270

Answers (5)

XMehdi01
XMehdi01

Reputation: 1

Instead of using list st = [] use a dict dt = {}. Also, in your case for performance looping over a set is better than going through all list elements, count method did this for you just loop over a set instead. At the end get index of maximum value of dictionary dt:

def freq(L):
    dt = {}
    for i in set(L):
        dt[i] = L.count(i)
    return max(dt, key=lambda k: dt[k])

print(freq([4, 6, 4, 3, 9, 1, 4])) #4

Upvotes: 0

yassine leo
yassine leo

Reputation: 11

I think this is the easiest way to solve it:

from statistics import mode

def freq(L):
    return mode(L) 

Upvotes: 0

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

Try dictionaries

def freq(L):
     st = {}
     for i in set(L):
        st.update({L.count(i):i})
     print (st.get(max(st)))

or shorter:

def freq(L):
    st={L.count(i):i for i in set(L)}
    print (st.get(max(st)))

another solution without dictionaries:

def freq(L):
    a=0
    b=None
    for e in set(L):
        if L.count(e)>a:
            a=L.count(e)
            b=e
    print(b)

Upvotes: 3

Carl
Carl

Reputation: 2067

Here is one way to do it with list comprehensions:

mostFrequentNumber = L[max([(L.count(d), i) for i, d in enumerate(L)])[1]]

To break it down:

We iterate an enumerated version of the list: i, d in enumerate(L), which will produce tuples with the value and index. Then we create a list of tuples with the count of d and the index, i: (L.count(d), i). We use the Max function to get the one with the highest count. Then we index the list L using the highest value at index [1] in the tuple.

This input: L = [1, 1, 1, 2, 3, 4] will then produce 1 as a result, although may not be the most efficient.

Upvotes: 1

jpp
jpp

Reputation: 164623

list.count has O(n) complexity, given n values in your list. If there are m unique values, this means your algorithm will have minimum O(m x n) complexity. This isn't desirable.

One O(n) solution is to create a counting dictionary and increment values as you iterate:

lst = [1, 2, 3, 3, 3, 2, 1, 3, 3]

def freq(L):
    d = {}
    for num in L:
        d[num] = d.get(num, 0) + 1
    maxcount = max(d.values())
    return next(k for k, v in d.items() if v == maxcount)

freq(lst)  # 3

Of course, with collections.Counter the syntax is trivial:

from collections import Counter

def freq(L):
    return Counter(L).most_common()[0][0]

Upvotes: 1

Related Questions