0

Lets say I have the list below:

list1=[1,2,4,6,8,3,2,5,8,4,2]

I want to return the integer, 2, because 8 is the maximum value and there are two 8s in the list. How can I do this? Edit: I also want to assume that the maximum number in the list can be any negative or non-negative number including zero.

3 Answers 3

1

Well you can use something like this:

list1=[1,2,4,6,8,3,2,5,8,4,2]
print list1.count(max(list1))
Sign up to request clarification or add additional context in comments.

Comments

0
ans = 0
mx = 0
for x in list1:
    if x > mx:
        mx = x
        ans = 1
    elif x == mx :
        ans += 1

print ans

assume max number is bigger than 0, otherwise you should initial mx with the negative infinity

1 Comment

you can initial mx = list1[0] if the list1 is not empty
0
>>> list1=[1,2,4,6,8,3,2,5,8,4,2]
>>> x = max(list1)
>>> l = []
>>> for i in list1:
    if i == x:
        l.append(i)


>>> l
[8, 8]
>>> len(l)
2

OR

>>> list1=[1,2,4,6,8,3,2,5,8,4,2]
>>> x = max(list1)
>>> result = len(filter(lambda i: i == x, list1))
>>> result
2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.