{"id":26151,"date":"2017-10-26T20:19:15","date_gmt":"2017-10-26T14:49:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26151"},"modified":"2017-10-26T20:19:15","modified_gmt":"2017-10-26T14:49:15","slug":"java-programming-find-number-occurring-odd-number-times","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-find-number-occurring-odd-number-times\/","title":{"rendered":"JAVA 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 & 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 \u201cPRACTICE\u201d 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<p>Below are implementations of this best approach.<\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Program:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2FJava%20program%20to%20find%20the%20element%20occurring%20odd%20number%20of%20times%0A%20%0Aclass%20OddOccurance%20%0A%7B%0A%20%20%20%20int%20getOddOccurrence(int%20ar%5B%5D%2C%20int%20ar_size)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20i%3B%0A%20%20%20%20%20%20%20%20int%20res%20%3D%200%3B%0A%20%20%20%20%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20ar_size%3B%20i%2B%2B)%20%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20res%20%3D%20res%20%5E%20ar%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20res%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20OddOccurance%20occur%20%3D%20new%20OddOccurance()%3B%0A%20%20%20%20%20%20%20%20int%20ar%5B%5D%20%3D%20new%20int%5B%5D%7B2%2C%203%2C%205%2C%204%2C%205%2C%202%2C%204%2C%203%2C%205%2C%202%2C%204%2C%204%2C%202%7D%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20ar.length%3B%0A%20%20%20%20%20%20%20%20System.out.println(occur.getOddOccurrence(ar%2C%20n))%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20has%20been%20contributed%20by%20Mayank%20Jaiswal%0A\u201d message=\u201dJava Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output:<\/p>\n<pre>5<\/pre>\n<p><strong><br \/>\nTime Complexity: <\/strong>O(n)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>JAVA 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,1,2139],"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-26151","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-coding","category-java","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\/26151","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=26151"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26151\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}