Lesson guide
What this Python exercise practices
Count occurrences of a value is a beginner practice lesson that focuses on dsa, problem patterns, edge cases. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python functions
- Loops
- Lists
- Basic edge cases
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Related public exercises
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.
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.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.