{"id":1305,"date":"2016-07-06T07:01:59","date_gmt":"2016-07-06T07:01:59","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1305"},"modified":"2021-11-14T12:13:56","modified_gmt":"2021-11-14T12:13:56","slug":"write-a-java-program-to-find-frequency-count-for-an-item-in-the-array","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-find-frequency-count-for-an-item-in-the-array\/","title":{"rendered":"Java program to find frequency count for an array item"},"content":{"rendered":"<h3><strong>Problem Statement<\/strong><\/h3>\n<p>Given an unsorted array of n integers. Elements can be repeated multiple times in the array. Write a java program to find frequency count of an array item.<\/p>\n<p>&nbsp;<\/p>\n<p><span id=\"more-1387\"><\/span><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1389\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-Frequency-Count.png\" alt=\"Array Frequency Count\" width=\"1068\" height=\"575\" title=\"\"><\/p>\n<h2><\/h2>\n<h3><strong>Java Program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class ArrayFrequency {\r\n\r\n    public static void main(String[] args) {\r\n        int n;\r\n        Scanner input = null;\r\n        int[] a = null;\r\n        try {\r\n            input=new Scanner(System.in);\r\n            System.out.println(\"Enter size of the array:\");\r\n            n= input.nextInt();\r\n\r\n            if(n &gt; 0) {\r\n                a=new int[n];\r\n                System.out.println(\"Enter array elements:\");\r\n                for (int i = 0; i &lt; n; i++) {\r\n                    a[i] = input.nextInt();\r\n                }\r\n            }else{\r\n                System.out.println(\"Enter positive number.\");\r\n                System.exit(0);\r\n            }\r\n\r\n            System.out.println(\"Input Array:\");\r\n            printArray(a);\r\n\r\n            System.out.println(\"Enter the array element to \r\nfind frequency:\");\r\n            int item= input.nextInt();\r\n\r\n            int count = frequencyCount(a,item);\r\n\r\n            if(count == 0 ) {\r\n                System.out.println(\"Item NOT FOUND in the array\");\r\n                System.exit(0);\r\n            }\r\n            else{\r\n                System.out.println(\"Frequency count of item:\" + count );\r\n            }\r\n\r\n        }catch (InputMismatchException ime) {\r\n            System.out.println(\"Not a valid input\");\r\n        }\r\n        catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if(input!=null)\r\n            {input.close();}\r\n        }\r\n    }\r\n\r\n    private static void printArray(int[] a){\r\n        for (int anA : a) {\r\n            System.out.print(\"[ \" + anA + \" ]\");\r\n        }\r\n        System.out.println( \"\");\r\n    }\r\n\r\n    private static int frequencyCount(int[] a, int value) {\r\n        int frequency = 0;\r\n        for (int i=0; i&lt;a.length; i++)\r\n            if (a[i] == value)\r\n                ++frequency;\r\n        return frequency;\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Run output of the program:<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1390\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-Frequency-Count-Java-Program.png\" alt=\"Array Frequency Count Java Program\" width=\"1278\" height=\"553\" title=\"\"><\/p>\n<h3><strong>TestCase 1<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n6<br \/>\nEnter array elements:<br \/>\n45<br \/>\n33<br \/>\n68<br \/>\n45<br \/>\n27<br \/>\n104<br \/>\nInput Array:<br \/>\n[ 45 ][ 33 ][ 68 ][ 45 ][ 27 ][ 104 ]<br \/>\nEnter the array element to find frequency:<br \/>\n45<br \/>\nFrequency count of item:2<\/p>\n<h3><strong>TestCase 2<\/strong><\/h3>\n<p>Enter size of the array:<br \/>\n6<br \/>\nEnter array elements:<br \/>\n45<br \/>\n33<br \/>\n68<br \/>\n45<br \/>\n27<br \/>\n104<br \/>\nInput Array:<br \/>\n[ 45 ][ 33 ][ 68 ][ 45 ][ 27 ][ 104 ]<br \/>\nEnter the array element to find frequency:<br \/>\n99<br \/>\nItem NOT FOUND in the array<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1305\" data-postid=\"1305\" class=\"themify_builder_content themify_builder_content-1305 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement Given an unsorted array of n integers. Elements can be repeated multiple times in the array. Write a java program to find frequency count of an array item. &nbsp; Java Program public class ArrayFrequency { public static void main(String[] args) { int n; Scanner input = null; int[] a = null; try { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1305","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1305"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1305\/revisions"}],"predecessor-version":[{"id":21448,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1305\/revisions\/21448"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}