Search code examples
numpycountmaxmode

get every value with maximum count in a numpy array


For example, I have the following array:

x = [1,2,3,3,4,5,6,6,7,8]

I need the following output:

y = [3,6]

So, it is similar to mode but can return more than one value if more than one value has the same maximum count. What is an efficient way to do it? Thanks.


Solution

  • just use np.unique withreturn_counts = True

    u, c = np.unique(x, return_counts = True)
    y = u[c == c.max()]