{"id":27090,"date":"2018-01-02T22:10:40","date_gmt":"2018-01-02T16:40:40","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27090"},"modified":"2018-01-02T22:10:40","modified_gmt":"2018-01-02T16:40:40","slug":"java-algorithm-write-function-counts-number-times-given-int-occurs-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-write-function-counts-number-times-given-int-occurs-linked-list\/","title":{"rendered":"Java Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List"},"content":{"rendered":"<p>Given a singly linked list and a key, count number of occurrences of given key in linked list. For example, if given linked list is 1-&gt;2-&gt;1-&gt;2-&gt;1-&gt;3-&gt;1 and given key is 1, then output should be 4.<\/p>\n<p><strong>Algorithm:<\/strong><\/p>\n<pre>1. Initialize count as zero.\r\n2. Loop through each element of linked list:\r\n     a) If element data is equal to the passed number then\r\n        increment the count.\r\n3. Return count. \r\n<\/pre>\n<p><strong>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to count occurrences in a linked list<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Linked list Node*\/<br\/>    class Node<br\/>    {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d) {data = d; next = null; }<br\/>    }<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/> <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/* Counts the no. of occurences of a node<br\/>    (search_for) in a linked list (head)*\/<br\/>    int count(int search_for)<br\/>    {<br\/>        Node current = head;<br\/>        int count = 0;<br\/>        while (current != null)<br\/>        {<br\/>            if (current.data == search_for)<br\/>                count++;<br\/>            current = current.next;<br\/>        }<br\/>        return count;<br\/>    }<br\/> <br\/>    \/* Drier function to test the above methods *\/<br\/>    public static void main(String args[])<br\/>    {<br\/>        LinkedList llist = new LinkedList();<br\/> <br\/>        \/* Use push() to construct below list<br\/>          1-&gt;2-&gt;1-&gt;3-&gt;1  *\/<br\/>        llist.push(1);<br\/>        llist.push(2);<br\/>        llist.push(1);<br\/>        llist.push(3);<br\/>        llist.push(1);<br\/> <br\/>        \/*Checking count function*\/<br\/>        System.out.println(&quot;Count of 1 is &quot;+llist.count(1));<br\/>    }<br\/>}<br\/>\/\/ This code is contributed by Rajat Mishra<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>count of 1 is 3<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<br \/>\n<strong>Auxiliary Space:<\/strong> O(1)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Write a function that counts the number of times a given int occurs in a Linked List &#8211; Linked List &#8211; Given a singly linked list and a key <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139,79476,79478],"tags":[80781,80785,80783,80788,80240,80782,80786,80787,80790,80789,80784],"class_list":["post-27090","post","type-post","status-publish","format-standard","hentry","category-java","category-linked-list","category-singly-linked-list","tag-algorithm-to-count-the-number-of-nodes-in-linked-list","tag-c-program-to-count-repeated-number-in-an-array","tag-count-nodes-in-linked-list-c","tag-count-the-number-of-nodes-in-a-doubly-linked-list","tag-find-length-of-linked-list-java","tag-how-to-count-the-number-of-nodes-in-a-linked-list-java","tag-linked-list-count-java","tag-pseudo-code-to-delete-a-singly-linked-list","tag-pseudocode-to-delete-a-singly-linked-list","tag-write-a-program-to-count-the-number-of-times-an-item-is-present-in-a-linked-list","tag-write-a-python-program-that-accept-a-word-from-the-user-and-reverse-it"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27090"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27090\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}