2

e.g. if a list is: list = [1, 2, 3, 4, 4] how to count the amount of times number 4 appeared, while not specifying it. using max(list) or something like that.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
My name
  • 21
  • 2
  • what do you mean by "not specifying it"? – kaouther Nov 09 '20 at 14:17
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – buddemat Nov 09 '20 at 14:17
  • do you mean you can't use the max function? – boboo Nov 09 '20 at 14:17
  • Hope you can adapt `if` statement to implement counter with [this answer](https://stackoverflow.com/questions/25134995/list-comprehensions-in-python-to-compute-minimum-and-maximum-values-of-a-list). Hint: reset counter when the new maximum value has been found and increment counter, when the next item is equal to maximum item. – astentx Nov 09 '20 at 15:15
  • As there seems to be some confusion about this in the answers below: Could you please clarify what you mean by "highest number" in the question title? Is it the largest element or the element with the most occurences? I.e. for `[1,2,3,4,4,5]` is it `4` or `5`? – buddemat Nov 09 '20 at 16:28

4 Answers4

2

Try this using max:

l = [1,2,3,4,4]
num = max(l, key=lambda x:l.count(x))  # num will be 4

the you can get the count of num.

l.count(num)  # this wil returns 2

in the other hand as @buddemat said(Here), it is better to:

from collections import Counter      
                                                                                                                                                                                                                                         
l = [1,2,3,4,4]
c = Counter(l)  
c.most_common()[0]

will give you

(4,2) # 4 is a maximum number and 2 is number of occurrence.

Also note that:

DO NOT use list as a variable name, list is predefined in python and you will override its own functionality.

Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
  • 2
    For performance reasons, you better not call `count` for each element in the list, as this will scan the complete list once for each element. The Counter approach is much better performance-wise (cmp. https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item). – buddemat Nov 09 '20 at 14:29
  • Nice comment @buddemat. I will mention it in my answer. – Mehrdad Pedramfar Nov 09 '20 at 14:30
  • The task is not to count the most common number, but to count the highest number. – Mikhail Vladimirov Nov 09 '20 at 14:35
1

While straightforward solution is to first find the maximum and then count it, here is how you can do both in one pass:

import functools
list = [1, 2, 3, 4, 4]
(max, count) = functools.reduce (
  lambda x, y:
    (y, 1) if x [0] is None or x [0] < y
    else x if x [0] > y
    else (x [0], x [1] + 1),
  list,
  (None, 0))
print (max, count)

This solution returns None, 0 for empty list.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
0
def get_max_occ(l):    
    max_i = None
    for i in l:
        if max_i is not None:
            if i > max_i:
                max_i = i
                occ = 1
            else:
                occ += 1
        else:
            max_i = i
            occ = 1
    return occ
0

First, find the maximum and use count

l1 =  [1, 2, 3, 4, 4] 
maximum = max(l1)
count = l1.count(maximum)

You can replace the maximum function by yourself. If you don't like to use the built-in function by following the ways

def max(l1):
  maximum = l1[0]
  for el in l1:
   if maximum< el:
     maximum = el
  return maximum

The above function will check each element of the list iteratively.

def maximum(l1):
    if len(l1)  == 1:
        return l1[0]
    if len(l1) == 2:
        if l1[0]> l1[1]:
            return l1[0]
        else:
            return l1[1]
    
    else:
        
        max1 = maximum(l1[:len(l1)//2])
        max2 = maximum(l1[len(l1)//2:])
        if max1 > max2:
            return max1
        else:
            return max2

The above function is using the divide and conquer approach to find the maximum which time complexity is logn

Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45