Part of the series

Several example codes

~1 min read • Updated Oct 13, 2025

Program Overview

This Python program reads the elements of array a and a value x, then counts how many times x appears in the array.


Python Code:


def count_occurrences(arr: list[int], x: int) -> int:
    count = 0
    for item in arr:
        if item == x:
            count += 1
    return count

# Variable for reading array a
raw_input = input("Enter array elements separated by space: ")
a = list(map(int, raw_input.strip().split()))

# Variable for reading value x
x = int(input("Enter value for x: "))

# Count repetitions of x in array a
repeats = count_occurrences(a, x)
print(f"{x} occurs {repeats} times in the array.")

Sample Output:


Array input: 13 14 13 12 14 12 13 14 12 13  
x input: 14  
Output: 14 occurs 3 times in the array.

Written & researched by Dr. Shahin Siami