# How to Find the Most Common Element in a NumPy Array

## Problem Formulation and Solution Overview

This article will show you how to find the most common element in a NumPy Array.

To make it more interesting, we have the following running scenario:

Carrie heard that Creative Prints is hiring a Python coder. They require the interviewee to answer several coding questions: one is to provide several ways to find the most common element in a NumPy Array.

π¬ Question: How would we write code to find the most common element in a NumPy Array?

We can accomplish this task by one of the following options:

## Preparation

Before moving forward, please ensure that the NumPy library is installed.

Then, add the following code to the top of each script. This snippet will allow the code in this article to run error-free.

`import numpy as np `

## Method 1: Use bincount() and argmax()

This example uses `bincount()` to tally the occurrences of each element in an array of positive integers and `argmax()` to return the maximum values along an axis.

```max_value = np.bincount([4, 18, 2, 8, 3, 15, 14, 15, 20, 12, 6, 3, 15, 12, 13, 19, 14, 81, 23, 44]).argmax()
print(max_value)```

The above code calls in NumPy’s `bincount()` function and passes it one (1) argument, a random `list` of positive integers.

Then, `argmax()` is called to retrieve the maximum value in the `list` . The results save to `max_value` and are output to the terminal.

The output is correct! This number occurs 3 times in the `list`.

π‘Note: This option does not work with negative numbers or strings.

## Method 2: Use Collections.Counter

This example uses the `collections` library that provides container types allowing the coder to easily store and access data values. The `Counter()` function keeps a running tally of each element’s count.

``` from collections import Counter

values = np.array([4, 18, 2, 8, 3, 5, 14, 5, -81, 12, 6, 3, -81, 12, 13, -81, 14, -81, 23, 44])
max_value = Counter(values)
print(max_value.most_common(1))```

The above code imports Python’s built-in `collections` library and the `Counter()` function

Then, a NumPy Array is declared containing a `list` of positive and negative random numbers. This saves to `values`.

Next, the `collections.Counter()` is called and passed the variable `values` as an argument`.` The results save to `max_value`. If output to the terminal, at this point, `max_value `would contain the following.

Finally, `most_common` (a counter tool for quick tallies) is appended to `max_value `and is passed an argument of 1. This indicates to return the highest value.

A List of Tuples containing the number with the highest tally and the associated count returns and is output to the terminal.

In this case, -81 occurred the most times with 4 occurrences.

To break the Tuple out of the List, append `[0]` to the end.

` print(max_value.most_common(1)[0])`

This will result in the following output.

## Method 3: Use unique()

This example uses NumPy’s `unique()` function that sorts and returns the unique array elements with their respective counts in a List format.

```values = np.array([4, 18, 2, 8, 3, 5, 14, 5, -81, 12, 6, 3, -81, 12, 13, -81, 14, -81, 23, 44])
vals, counts = np.unique(values, return_counts=True)
print(vals)
print(counts)```

The above code declares a NumPy Array containing a `list `of positive and negative random numbers. This saves to `values`.

Next, `unique()` is called and passed two (2) arguments: the `values list `created above and `return_counts`. If set to True, this function will return the unique values and their respective occurrences. The results save to `vals `and `counts`.

The contents of `vals `and `counts `are output to the terminal using separate `print()` statements.

To retrieve the most comment element and its respective count, use slicing.

`print(vals[0], counts[0])`

The output from the above is as follows.

## Method 4: Use unique() and zip()

This example uses NumPy’s `unique()` and the `zip()` function, which takes an arbitrary number of iterables and returns a Dictionary based on number of occurrences as the key and total count as the value.

```values = np.array([4, 18, 2, 8, 3, 5, 14, 5, -81, 12, 6, 3, -81, 12, 13, -81, 14, -81, 23, 44])
result = dict(zip(*np.unique(values, return_counts=True)))
print(result)```

The above code declares a NumPy Array containing a `list `of positive and negative random numbers. This saves to `values`.

Then, `zip()` is called and passed one (1) argument, `unique()`, which passes two (2) arguments: the `values list `created above and `return_counts`. If `return_counts `is set to `True`, this will return the unique values and their respective occurrences.

Next, `dict()` is called which merges the data above into a `Dictionary `format. This saves to `result` and is output to the terminal.

To retrieve the most common element, run the following code.

`print(list(result.items())[0:1])`

The output from the above is as follows.

## Method 5: Use mode()

This example uses `mode()` from the `statistics` library. This function returns the single most common element found in the passed argument.

```from statistics import mode

def most_common(List):
return(mode(List))

values = np.array([4, 18, 2, 8, 3, 5, 14, 5, -81, 12, 6, 3, -81, 12, 13, -81, 14, -81, 23, 44])
print(most_common(values))```

The above code calls in `mode()` from the statistics library.

Then, a definition is defined called `most_common` and accepts one (1) argument, a `list`. The `mode()` function is applied to the `list` and is returned.

Next, a NumPy Array is declared containing random numbers and saves to values.

The above `most_common` function is called, passing the NumPy Array values and output to the terminal.

## Summary

This article has provided five (5) ways to find the most common element in a NumPy Array. These examples should give you enough information to select the best fitting for your coding requirements.

Good Luck & Happy Coding!