
How to Count the Occurrence of a Number in an Array Using JavaScript?
Jun 03, 2025 2 Min Read 420 Views
(Last Updated)
In programming, one of the most common tasks is working with arrays, especially searching for elements and to count the occurrence of a number. Whether you’re preparing for coding interviews or solving real-world problems, mastering array operations is essential.
In this article, we tackle a simple yet practical challenge: to count the occurrence of a number in an Array using JavaScript
We’ll walk through a clear problem statement, analyze the input and output requirements, and finally explore a clean solution using built-in JavaScript functions.
Table of contents
- How to Count the Occurrence of a Number in an Array Using JavaScript?
- Problem Description
- Your task:
- Input Format
- Output Format
- Sample Test Case
- JavaScript Solution
- How the Code Works
- Conclusion
How to Count the Occurrence of a Number in an Array Using JavaScript?
Given a number K and an array of N elements, how often does K appear in the array? If the number is not present at all, return -1.
Problem Description
You are given:
- Two integers:
- N — the number of elements in the array
- K — the number whose repetitions you want to count
- N — the number of elements in the array
- A list of N integers.
Your task:
- Count how many times K appears in the array.
- If K is not found at all, output -1.
Input Format
- First line: Two space-separated integers N and K.
- Second line: N space-separated integers representing the array.
Output Format
- Print a single number:
- The count of how many times K appears in the array.
- If K is not present at all, print -1.
- The count of how many times K appears in the array.
Sample Test Case
Input
- 6 2
- 1 2 3 5 7 8
Output
- 1
Explanation:
In this example, K is 2, and the array is [1, 2, 3, 5, 7, 8]. The number 2 appears once in the array, so the output is 1.
JavaScript Solution
If you’re using a coding platform to code JavaScript that accepts standard input/output (such as HackerRank or CodeChef), the following solution will work as expected:
const readline = require(‘readline’); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let inputLines = []; rl.on(‘line’, function(line) { inputLines.push(line); if (inputLines.length === 2) { let [n, k] = inputLines[0].split(‘ ‘).map(Number); let arr = inputLines[1].split(‘ ‘).map(Number); let count = arr.filter(x => x === k).length; if (count === 0) { console.log(‘-1’); } else { console.log(count); } rl.close(); } }); |
How the Code Works
- Reading Input
The program reads two lines of input:
- The first line contains N and K.
- The second line contains the array elements.
- The first line contains N and K.
- Processing
The array is processed using Array.prototype.filter() to count how often K appears. - Output
- If the count is zero, it prints -1.
- Otherwise, it prints the number of occurrences of K.
- If the count is zero, it prints -1.
If you want to learn more about JavaScript and how it enables developers to work seamlessly, consider enrolling in GUVI’s IIT-M Pravartak-certified Full Stack Development Course that not only teaches you the basics but also makes you an experienced developer through hands-on projects guided by an actual mentor.
Conclusion
This problem is an excellent exercise for developing a deeper understanding of:
- Reading and parsing structured input
- Filtering and processing arrays
- Handling edge cases effectively
Did you enjoy this article?