{"id":80034,"date":"2025-05-22T16:36:13","date_gmt":"2025-05-22T11:06:13","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=80034"},"modified":"2025-09-09T16:57:10","modified_gmt":"2025-09-09T11:27:10","slug":"count-the-occurrence-of-a-number-in-an-array-using-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/count-the-occurrence-of-a-number-in-an-array-using-javascript\/","title":{"rendered":"How to Count the Occurrence of a Number in an Array Using JavaScript?"},"content":{"rendered":"\n<p>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&#8217;re preparing for coding interviews or solving real-world problems, mastering array operations is essential. <\/p>\n\n\n\n<p>In this article, we tackle a simple yet practical challenge: to count the occurrence of a number in an Array using JavaScript<\/p>\n\n\n\n<p>We&#8217;ll walk through a clear problem statement, analyze the input and output requirements, and finally explore a clean solution using built-in JavaScript functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Count the Occurrence of a Number in an Array Using JavaScript?<\/strong><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Problem Description<\/strong><\/h3>\n\n\n\n<p>You are given:<\/p>\n\n\n\n<ul>\n<li>Two integers:<br>\n<ul>\n<li>N \u2014 the number of elements in the <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-arrays-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">array<\/a><br><\/li>\n\n\n\n<li>K \u2014 the number whose repetitions you want to count<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A list of N integers.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Your task:<\/strong><\/h3>\n\n\n\n<ul>\n<li>Count how many times K appears in the array.<br><\/li>\n\n\n\n<li>If K is not found at all, output -1.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Input Format<\/strong><\/h3>\n\n\n\n<ul>\n<li>First line: Two space-separated integers N and K.<br><\/li>\n\n\n\n<li>Second line: N space-separated integers representing the array.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output Format<\/strong><\/h3>\n\n\n\n<ul>\n<li>Print a single number:<br>\n<ul>\n<li>The count of how many times K appears in the array.<br><\/li>\n\n\n\n<li>If K is not present at all, print -1.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sample Test Case<\/strong><\/h2>\n\n\n\n<p><strong>Input<\/strong><\/p>\n\n\n\n<ul>\n<li>6 2<\/li>\n\n\n\n<li>1 2 3 5 7 8<\/li>\n<\/ul>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<ul>\n<li>1<\/li>\n<\/ul>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong> 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JavaScript Solution<\/strong><\/h2>\n\n\n\n<p>If you&#8217;re using a<a href=\"https:\/\/www.guvi.in\/blog\/top-websites-to-practice-coding\/\" target=\"_blank\" rel=\"noreferrer noopener\"> coding platform<\/a> to code <a href=\"https:\/\/www.guvi.in\/hub\/javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> that accepts standard input\/output (such as <a href=\"https:\/\/www.hackerrank.com\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">HackerRank<\/a> or CodeChef), the following solution will work as expected:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>const<\/strong> readline = require(&#8216;readline&#8217;);<br><br><strong>const<\/strong> rl = readline.createInterface({<br>&nbsp; &nbsp; input: process.stdin,<br>&nbsp; &nbsp; output: process.stdout<br>});<br><br><strong>let<\/strong> inputLines = [];<br><br>rl.on(&#8216;line&#8217;, <strong>function<\/strong>(line) {<br>&nbsp; &nbsp; inputLines.push(line);<br><br>&nbsp; &nbsp; <strong>if<\/strong> (inputLines.length === 2) {<br>&nbsp; &nbsp; &nbsp; &nbsp; <strong>let<\/strong> [n, k] = inputLines[0].split(&#8216; &#8216;).map(Number);<br>&nbsp; &nbsp; &nbsp; &nbsp; <strong>let<\/strong> arr = inputLines[1].split(&#8216; &#8216;).map(Number);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; <strong>let<\/strong> count = arr.filter(x =&gt; x === k).length;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; <strong>if<\/strong> (count === 0) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(&#8216;-1&#8217;);<br>&nbsp; &nbsp; &nbsp; &nbsp; } <strong>else<\/strong> {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(count);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; rl.close();<br>&nbsp; &nbsp; }<br>});<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>JavaScript Solution<\/strong><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How the Code Works<\/strong><\/h3>\n\n\n\n<ul>\n<li><strong>Reading Input<\/strong><strong><br><\/strong> The program reads two lines of input:<br>\n<ul>\n<li>The first line contains N and K.<br><\/li>\n\n\n\n<li>The second line contains the array elements.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Processing<\/strong><strong><br><\/strong> The array is processed using Array.prototype.filter() to count how often K appears.<br><\/li>\n\n\n\n<li><strong>Output<\/strong><strong><br><\/strong>\n<ul>\n<li>If the count is zero, it prints -1.<br><\/li>\n\n\n\n<li>Otherwise, it prints the number of occurrences of K.<br><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about JavaScript and how it enables developers to work seamlessly, consider enrolling in HCL GUVI\u2019s IIT-M Pravartak-certified<strong> <\/strong><a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=count-the-occurrence-of-a-number-in-an-array\" target=\"_blank\" rel=\"noreferrer noopener\">Full Stack Development Course<\/a> that not only teaches you the basics but also makes you an experienced developer through hands-on projects guided by an actual mentor.\u00a0<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/new-array-and-object-methods-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\">Exploring the New Array and Object Methods in JavaScript<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>This problem is an excellent exercise for developing a deeper understanding of:<\/p>\n\n\n\n<ul>\n<li>Reading and parsing structured input<br><\/li>\n\n\n\n<li>Filtering and processing arrays<br><\/li>\n\n\n\n<li>Handling edge cases effectively<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":80806,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[429,294],"tags":[],"views":"2156","authorinfo":{"name":"Arun Kumar","url":"https:\/\/www.guvi.in\/blog\/author\/arun-kumar\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/How-to-Count-the-Occurrence-of-a-Number-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/05\/How-to-Count-the-Occurrence-of-a-Number.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80034"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=80034"}],"version-history":[{"count":8,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80034\/revisions"}],"predecessor-version":[{"id":86766,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/80034\/revisions\/86766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/80806"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=80034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=80034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=80034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}