get max duplicate item in list

I have this list:

mylist = [20, 30, 25, 20, 30]

After getting the duplicated values indexes using

[i for i, x in enumerate(mylist) if mylist.count(x) > 1]

the result is:

`[0, 1, 3, 4]` 

having two pairs of duplicated values. I'd like to know, how can i get only the higher duplicated value? In this list it is 30 or any of it's indexes, 1 or 4, instead of the whole list of duplicated values.

Regards...

This one is O(n)

>>> from collections import Counter
>>> mylist = [20, 30, 25, 20, 30]
>>> max(k for k,v in Counter(mylist).items() if v>1)
30

Python, Make a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus,� Syntax. =MAX (IF (<value to lookup in list> = <unique value>,<range of date for all values>)) To create this as an Array Function press Ctrl + Shift + Enter rather than just Enter. You can tell an Array Function by the curly brackets around the entire request (including equals) {=MAX (IF (A:A=D2,B:B))} Example.

Getting the maximum duplicated value:

max(x for x in mylist if mylist.count(x) > 1)

This has O(n**2) performance because of the repeated count() calls, unfortunately. Here's a wordier way to do the same thing that will have O(n) performance, important if the list is long:

seen = set()
dups = set()
for x in mylist:
    if x in seen:
        dups.add(x)
    seen.add(x)
max_dups = max(dups)

Python : How to get all keys with maximum value in a Dictionary , print('Maximum Value in Dictionary : ', itemMaxValue[1]). listOfKeys = list(). # Iterate over all the items in dictionary to find keys with max value. To get the nth largest value in a data set with duplicates, you can use an array formula based on the MAX and IF functions. Note: the LARGE function will easily return nth values, but LARGE will return duplicates when they exist in the source data. In the example shown, the formula in E6 is: =

Another O(n) way of doing it, just because...

>>> from collections import defaultdict
>>> 
>>> mylist = [20,30,25,20,30]
>>> dd = defaultdict(int)
>>> for i in mylist:
...    dd[i] += 1
...
>>> max(i for i in dd if dd[i] > 1)
30

You can also do it using a regular old dict:

>>> d = dict.fromkeys(mylist, 0)
>>> for i in mylist:
...   d[i] += 1
... 
>>> max(i for i in d if d[i] > 1)
30

Python: Find duplicates in a list with frequency count & index , Get frequency count of duplicate elements in the given list '''. dictOfElems = dict(). # Iterate over each element in list. for elem in listOfElems:. I want "dfg" as the output because this item is repeated maximum times. Can anyone help in this. c# linq list count max. copy and paste this URL into your RSS reader.

$ cat /tmp/1.py
from itertools import groupby

def find_max_repeated(a):
    a = sorted(a, reverse = True)
    for k,g in groupby(a):
        gl = list(g)
        if len(gl) > 1:
            return gl[0]

a = [1,1,2,3,3,4,5,4,6]
print find_max_repeated(a)

$ python /tmp/1.py
4

How to find the most repeated integer in a list in python, Line 8 is finding the largest count - normally calling max on a dictionary will value; so the key argument to the max function tells max how to get the items to be� The article shows a few different ways to get the max value in Excel based on one or several conditions that you specify. In our previous tutorial, we looked at the common uses of the MAX function which is designed to return the largest number in a dataset.

Just some relative timings to consider:

from collections import Counter
from collections import defaultdict

mylist = [20, 30, 25, 20, 30]

def f1():
    seen = set()
    dups = set()
    for x in mylist:
        if x in seen:
            dups.add(x)
        seen.add(x)
    max_dups = max(dups)

def f2():
    max(x for x in mylist if mylist.count(x) > 1)

def f3():
    max(k for k,v in Counter(mylist).items() if v>1)

def f4():
    dd = defaultdict(int)
    for i in mylist:
        dd[i] += 1

    max(i for i in dd if dd[i] > 1)

def f5():
    d = dict.fromkeys(mylist, 0)            
    for i in mylist:
       d[i] += 1

    max(i for i in d if d[i] > 1)

cmpthese([f1,f2,f3,f4,f5])    

prints:

   rate/sec     f3     f4     f5     f2     f1
f3   93,653     -- -63.3% -73.0% -79.2% -83.6%
f4  255,137 172.4%     -- -26.3% -43.3% -55.3%
f5  346,238 269.7%  35.7%     -- -23.1% -39.3%
f2  450,356 380.9%  76.5%  30.1%     -- -21.0%
f1  570,419 509.1% 123.6%  64.7%  26.7%     --

So choose wisely

How to find max or min value based on unique values in Excel?, If you have a list with some duplicate values, and now you just want to find out the unique max or min value from this list as below screenshot shown, in this case,� Python List max() Method - Python list method max returns the elements from the list with maximum value.

How to remove duplicates but leaving lowest value in another , Remove duplicate but keep lowest/highest value with Kutools for Excel good Then select the values in the pasted list, and click Data > Remove Duplicates,� Approach #2 : Pythonic Naive appraoch Make a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus, we find the maximum out of it.

Python max() Function, Definition and Usage. The max() function returns the item with the highest value, or the item with the highest value in an iterable. If the values are strings,� Python Remove Duplicates From List : 4 Ways Of Removing Duplicates From List. Python provides various ways to remove duplicates from list. You can use set() function or write the user-defined function that can remove the duplicate items and gives the unique items list. Let’s see them one by one.

Excel formula: nth largest value with duplicates, To get the nth largest value in a data set with duplicates, you can use an array formula based on the MAX and IF functions. Note: the LARGE function will easily � Create a Function. If you like to have a function where you can send your lists, and get them back without duplicates, you can create a function and insert the code from the example above.

Comments
  • Based on these timings, this is the slowest solution posted. Ned Batchelder's is 5X faster.
  • That is not O(n); that is O(n*log(n)); the same as with sorting
  • @IgorChubin Python uses a hash table for sets. Insertion and lookup is on average in O(1). I'm not sure if max is O(1) or O(n). I did not find a documentation if max is optimized for dicts. Either way: This algorithm should work in O(n).
  • @kay: Why do you think that lookup in a set is O(1) and not O(log n)?
  • @IgorChubin Average lookup time is O(1) and worst case is O(N) where all hashed values collide.
  • @jamylak: (@kay, @NedBatchelder) You are right, that was my fault
  • Interesting how you have used fromkeys instead of the setdefault which is the usual alternate approach to defaultdict.
  • Maybe you could add some explanation … (after all this is a Q&A site.)
  • The use of sorted makes this O(n log n)