Python Program to Count Occurrence of an Element in an Array.

Given an array of integers of size n and a target element. Your task is to write a Python program to count the number of occurrences of the target element in the array.

Example:
Input: arr = [4, 2, 8, 3, 2, 7, 5, 2, 1, 2, 9, 2] target = 2
Output: The element 2 occurs 5 times in the array.

Input: arr = [2, 8, 3, 3, 7, 5, 2, 1, 2, 2] target = 3
Output: The element 3 occurs 2 times in the array.

There are multiple methods by which we can count the occurrence of an element in a Python Array. Let's learn a few of them in this article.

Count the Occurrence of Elements in an Array Using Linear Search.

In this approach, we iterate through the array linearly, counting occurrences of the target element.

Algorithm Steps:
  • Initialize a counter variable to zero, which will be used to store the count of occurrences.
  • Iterate through the array element by element.
  • For each element encountered, check if it matches the target element for which we are counting occurrences.
  • If the current element matches the target element, increment the counter.
  • After the iteration, the counter variable contains the total count of occurrences.

Python Code:
# Python code to count occurrence of an element
def count_occurrences(arr, target):
    count = 0
    for element in arr:
        if element == target:
            count += 1
    return count
    
# drive
arr = [2, 4, 5, 2, 2, 5, 1]
target = 2
print('{} has occurred {} times'.format(target, count_occurrences(arr, target)))
Output:
2 has occurred 3 times
  • Time Complexity: O(n) where n is the length of the array.
  • Space Complexity: O(1) as we use only a constant amount of extra space.

Count Occurrence of an Element in Array Using Counter Function.

This approach leverages Python's Counter from the collections module, streamlining the counting process. The counter generates a dictionary where keys represent elements and values represent their counts.

Algorithm Steps:
  • Import Counter function from Collection module.
  • Apply the Counter function to the array to create a dictionary of element counts.
  • Retrieve the count of the target element from the generated dictionary.

Python Code:
# Python code to count occurrence of an element using counter
from collections import Counter

def countTarget(arr, target):
    counts = Counter(arr)
    return counts[target]
    
# drive
arr = [2, 4, 5, 2, 2, 5, 1]
target = 2
print('{} has occurred {} times'.format(target, countTarget(arr, target)))
Output:
2 has occurred 3 times
  • Time Complexity: O(n) where n is the length of the array.
  • Space Complexity: O(n) where n is the number of unique elements in the array.

Count Occurrence of an Element in an Array Using list.count().

Python provides a built-in count() method for lists, offering a concise solution to count occurrences. However, it involves iterating through the entire list internally, leading to a higher time complexity.

Python Code:
# Python code to count occurrence of an element in list
def countTarget(arr, target):
    return arr.count(target)
    
# drive
arr = [2, 4, 5, 2, 2, 5, 1, 1]
target = 1
print('{} has occurred {} times'.format(target, countTarget(arr, target)))
Output:
2 has occurred 3 times
  • Time Complexity: O(n^2) where n is the length of the array.
  • Space Complexity: O(1) as only a constant amount of extra space is required.

In summary, the linear search, Counter, and list.count() approaches provide varied ways to count element occurrences in Python. The selection of a method depends on the specific needs of the application.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS