Python: Get the number of occurrences of a specified element in an array
6. Count occurrences of a specified element in an array
Write a Python program to get the number of occurrences of a specified element in an array.
Pictorial Presentation:

Sample Solution:
Python Code :
from array import *
array_num = array('i', [1, 3, 5, 3, 7, 9, 3])
print("Original array: "+str(array_num))
print("Number of occurrences of the number 3 in the said array: "+str(array_num.count(3)))
Sample Output:
Original array: array('i', [1, 3, 5, 3, 7, 9, 3]) Number of occurrences of the number 3 in the said array: 3
For more Practice: Solve these Related Problems:
- Write a Python program to count the occurrences of a target number in an array using the count() method.
- Write a Python program to iterate through an array and count the number of times a specified element appears without using count().
- Write a Python program to use list comprehension to filter and count elements in an array that match a given value.
- Write a Python function that takes an array and a target value and returns the occurrence count of that value.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to get the current memory address and the length in elements of the buffer used to hold an array’s contents and also find the size of the memory buffer in bytes.
Next: Write a Python program to append items from inerrable to the end of the array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics