{"id":26141,"date":"2017-10-26T20:10:53","date_gmt":"2017-10-26T14:40:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26141"},"modified":"2017-10-26T20:10:53","modified_gmt":"2017-10-26T14:40:53","slug":"c-program-find-number-occurring-odd-number-times","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-find-number-occurring-odd-number-times\/","title":{"rendered":"C Programming-Find the Number Occurring Odd Number of Times"},"content":{"rendered":"<p>Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time &amp; constant space.<\/p>\n<p><strong>Example:<\/strong><br \/>\nI\/P = [1, 2, 3, 2, 3, 1, 3]\nO\/P = 3<\/p>\n<p><strong>Recommended:<\/strong> <strong>Please solve it on &#8220;PRACTICE&#8221; first, before moving on to the solution.<\/strong><\/p>\n<p>A <strong>Simple Solution<\/strong> is to run two nested loops. The outer loop picks all elements one by one and inner loop counts number of occurrences of the element picked by outer loop. Time complexity of this solution is O(n2).<\/p>\n<p>A <strong>Better Solution<\/strong> is to use Hashing. Use array elements as key and their counts as value. Create an empty hash table. One by one traverse the given array elements and store counts. Time complexity of this solution is O(n). But it requires extra space for hashing.<\/p>\n<p>The <strong>Best Solution<\/strong> is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with 0 is x.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Below are implementations of this best approach.<\/p>\n<p><strong>Program:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C Programming<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/C program to find the element occurring odd number of times<br\/> <br\/>#include &lt;stdio.h&gt;<br\/>int getOddOccurrence(int ar[], int ar_size)<br\/>{<br\/>     int i;<br\/>     int res = 0; <br\/>     for (i=0; i &lt; ar_size; i++)     <br\/>        res = res ^ ar[i];<br\/>      <br\/>     return res;<br\/>}<br\/> <br\/>\/* Diver function to test above function *\/<br\/>int main()<br\/>{<br\/>     int ar[] = {2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};<br\/>     int n = sizeof(ar)\/sizeof(ar[0]);<br\/>     printf(&quot;%d&quot;, getOddOccurrence(ar, n));<br\/>     return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>5<\/pre>\n<p><strong><br \/>\nTime Complexity: <\/strong>O(n)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Find the Number Occurring Odd Number of Times &#8211; Bit Algorithm &#8211; Given an array of positive integers. All numbers occur even number of times <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852,69866,1],"tags":[77577,77568,77534,77567,77571,77562,77570,77576,77541,77553,77572,77554,77558,77530,77540,77544,77565,77535,77536,77548,75529,77569,77549,77551,77552,77537,77556,77557,77532,77545,77546,77559,77543,77542,77560,77533,77531,77539,77547,77563,77575,77564,77555,77573,77574,75528,77550,77538,77561,77566],"class_list":["post-26141","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-c-programming","category-coding","tag-1-2-3-4-5-n","tag-2-dice-roller","tag-4-sided-dice","tag-4-sided-die","tag-5-sided-dice","tag-6-sided-dice","tag-6-sided-die","tag-8-sided-cube","tag-coin-flip-probability","tag-coin-land","tag-dice-1","tag-dice-4","tag-dice-six-sided","tag-equally-likely-events","tag-even-numbers-list","tag-example-of-theoretical-probability","tag-experimental-probability-examples","tag-find-a-number","tag-find-this-number","tag-finding-mean-median-and-mode","tag-flip-a-coin","tag-four-sided-die","tag-how-do-you-find-the-mean-median-and-mode","tag-how-do-you-find-the-median","tag-how-to-find-out-the-median","tag-median-number","tag-number-dice","tag-number-probability","tag-odd-no","tag-odd-numbers","tag-odd-numbers-list","tag-probability-2-dice","tag-probability-experiment","tag-probability-of-coin-toss","tag-probability-of-tossing-a-coin-4-times","tag-roll-a-die","tag-roll-five-dice","tag-roll-number","tag-roll-of-dice","tag-rolling-dice-probability","tag-six-sided-dice","tag-six-sided-dice-simulator","tag-theoretical-probability-calculator","tag-theoretical-probability-examples","tag-theoretical-probability-formula","tag-toss-a-coin","tag-what-is-mean-median-and-mode","tag-what-is-probability","tag-what-is-the-theoretical-probability","tag-whats-the-mode-in-math"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26141","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=26141"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26141\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}