Count occurrences of a number
EasyPractice library · +10 XPbasicsloopsarrays
Given a list of N integers, find the number of times a specific integer K appears in the list. Print this count.
Input
The first line contains two integers, N and K, separated by a space. The second line contains N integers, representing the list, separated by spaces.
Output
A single integer representing the count of K in the list.
Constraints
1 <= N <= 1000 -1000 <= each integer in the list <= 1000 -1000 <= K <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 3 1 3 2 3 4
Output
2
K=3 appears 2 times in the list.
Example 2
Input
7 5 1 2 3 4 6 7 8
Output
0
K=5 does not appear in the list, so the count is 0.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 3 1 3 2 3 4
Example 2 input
7 5 1 2 3 4 6 7 8