w3resource logo

:

Python Exercises

Python: Find the most frequent value in an array

Python NumPy: Random Exercise-13 with Solution

Write a Python program to find the most frequent value in an array.

Sample Solution:-

Python Code:

import numpy as np
x = np.random.randint(0, 10, 40)
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print(np.bincount(x).argmax())

Sample Output:

Original array:                                                        
[4 7 3 8 4 8 6 3 1 8 6 3 3 9 7 8 3 3 0 6 8 3 1 8 2 2 9 7 8 9 8 5 9 6 8 
5 3                                                                    
 8 9 1]                                                                
Most frequent value in the above array:                                
8 

Python Code Editor:

Improve this sample solution and post your code through Disqus