Count Occurrences

Count how many times element appears.

Logic BuildingIntermediate
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

x = int(input("Enter element to count: "))

# Count occurrences
count = arr.count(x)
print(f"{x} appears {count} times")

Output

Enter array size: 6
Element 1: 5
Element 2: 3
Element 3: 5
Element 4: 7
Element 5: 5
Element 6: 2
Enter element to count: 5
5 appears 3 times

Use count() method to count occurrences.

Key Concepts:

  • count(x) returns number of occurrences
  • Counts all matches
  • Simple and efficient