Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Compute array sumE02. Find the maximum and minimum in an arrayE03. Count occurrences of a valueE

Problem No 3

Count occurrences of a value

Easy

8 minute session

Summary

Count how many times a target value appears in an array.

Problem statement

Given a list arr and a target value val, return the number of times val appears in arr. The function should work for any Python object (ints, strings, None, etc.). Time complexity O(n).

Task

Create a function that returns the number of occurrences of a given value in a list. Practice equality checks and iteration.

Examples

Basic example

Input

arr = [1, 2, 2, 3, 2], val = 2

Output

3

Explanation

2 appears three times in the list.

Input format

Two arguments: (list, value). Example: ([1,2,3,2], 2)

Output format

An integer count of how many times value occurs in the list.

Constraints

List length 0..10^6. Equality check uses Python's ==. Time O(n).

Samples

Sample input 0

["a", "b", "a"], "a"

Sample output 0

2

Explanation 0

"a" appears twice.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:49 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.