{"id":25711,"date":"2017-10-25T20:09:03","date_gmt":"2017-10-25T14:39:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25711"},"modified":"2017-10-25T20:09:03","modified_gmt":"2017-10-25T14:39:03","slug":"find-element-appears","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-element-appears\/","title":{"rendered":"Find the element that appears once"},"content":{"rendered":"<p>Given an array where every element occurs three times, except one element which occurs only once. Find the element that occurs once. Expected time complexity is O(n) and O(1) extra space.<br \/>\n<strong>Examples:<\/strong><\/p>\n<p>We can use sorting to do it in O(nLogn) time. We can also use hashing, but the worst case time complexity of but hashing requires extra space.<\/p>\n<p>The idea is to use bitwise operators for a solution that is O(n) time and uses O(1) extra space. The solution is not easy like other XOR based solutions, because all elements appear odd number of times here. The idea is taken from here.<\/p>\n<p>Run a loop for all elements in array. At the end of every iteration, maintain following two values.<\/p>\n<p>ones: The bits that have appeared 1st time or 4th time or 7th time .. etc.<\/p>\n<p>twos: The bits that have appeared 2nd time or 5th time or 8th time .. etc.<\/p>\n<p>Finally, we return the value of \u2018ones\u2019<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>How to maintain the values of \u2018ones\u2019 and \u2018twos\u2019?<br \/>\n\u2018ones\u2019 and \u2018twos\u2019 are initialized as 0. For every new element in array, find out the common set bits in the new element and previous value of \u2018ones\u2019. These common set bits are actually the bits that should be added to \u2018twos\u2019. So do bitwise OR of the common set bits with \u2018twos\u2019. \u2018twos\u2019 also gets some extra bits that appear third time. These extra bits are removed later.<br \/>\nUpdate \u2018ones\u2019 by doing XOR of new element with previous value of \u2018ones\u2019. There may be some bits which appear 3rd time. These extra bits are also removed later.<\/p>\n<p>Both \u2018ones\u2019 and \u2018twos\u2019 contain those extra bits which appear 3rd time. Remove these extra bits by finding out common set bits in \u2018ones\u2019 and \u2018twos\u2019.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/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\">#include &lt;stdio.h&gt;<br\/> <br\/>int getSingle(int arr[], int n)<br\/>{<br\/>    int ones = 0, twos = 0 ;<br\/> <br\/>    int common_bit_mask;<br\/> <br\/>    \/\/ Let us take the example of {3, 3, 2, 3} to understand this<br\/>    for( int i=0; i&lt; n; i++ )<br\/>    {<br\/>        \/* The expression &quot;one &amp; arr[i]&quot; gives the bits that are<br\/>           there in both &#039;ones&#039; and new element from arr[].  We<br\/>           add these bits to &#039;twos&#039; using bitwise OR<br\/> <br\/>           Value of &#039;twos&#039; will be set as 0, 3, 3 and 1 after 1st,<br\/>           2nd, 3rd and 4th iterations respectively *\/<br\/>        twos  = twos | (ones &amp; arr[i]);<br\/> <br\/> <br\/>        \/* XOR the new bits with previous &#039;ones&#039; to get all bits<br\/>           appearing odd number of times<br\/> <br\/>           Value of &#039;ones&#039; will be set as 3, 0, 2 and 3 after 1st,<br\/>           2nd, 3rd and 4th iterations respectively *\/<br\/>        ones  = ones ^ arr[i];<br\/> <br\/> <br\/>        \/* The common bits are those bits which appear third time<br\/>           So these bits should not be there in both &#039;ones&#039; and &#039;twos&#039;.<br\/>           common_bit_mask contains all these bits as 0, so that the bits can <br\/>           be removed from &#039;ones&#039; and &#039;twos&#039;   <br\/> <br\/>           Value of &#039;common_bit_mask&#039; will be set as 00, 00, 01 and 10<br\/>           after 1st, 2nd, 3rd and 4th iterations respectively *\/<br\/>        common_bit_mask = ~(ones &amp; twos);<br\/> <br\/> <br\/>        \/* Remove common bits (the bits that appear third time) from &#039;ones&#039;<br\/>             <br\/>           Value of &#039;ones&#039; will be set as 3, 0, 0 and 2 after 1st,<br\/>           2nd, 3rd and 4th iterations respectively *\/<br\/>        ones &amp;= common_bit_mask;<br\/> <br\/> <br\/>        \/* Remove common bits (the bits that appear third time) from &#039;twos&#039;<br\/> <br\/>           Value of &#039;twos&#039; will be set as 0, 3, 1 and 0 after 1st,<br\/>           2nd, 3rd and 4th itearations respectively *\/<br\/>        twos &amp;= common_bit_mask;<br\/> <br\/>        \/\/ uncomment this code to see intermediate values<br\/>        \/\/printf (&quot; %d %d \\n&quot;, ones, twos);<br\/>    }<br\/> <br\/>    return ones;<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    int arr[] = {3, 3, 2, 3};<br\/>    int n = sizeof(arr) \/ sizeof(arr[0]);<br\/>    printf(&quot;The element with single occurrence is %d &quot;,<br\/>            getSingle(arr, n));<br\/>    return 0;<br\/>}<br\/>Run on IDE<br\/>Output:<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>2<\/pre>\n<p>Time Complexity: O(n)<br \/>\nAuxiliary Space: O(1)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is another O(n) time complexity and O(1) extra space method suggested by aj. We can sum the bits in same positions for all the numbers and take modulo with 3. The bits for which sum is not multiple of 3, are the bits of number with single occurrence.<br \/>\nLet us consider the example array {5, 5, 5, 8}. The 101, 101, 101, 1000<br \/>\nSum of first bits%3 = (1 + 1 + 1 + 0)%3 = 0;<br \/>\nSum of second bits%3 = (0 + 0 + 0 + 0)%0 = 0;<br \/>\nSum of third bits%3 = (1 + 1 + 1 + 0)%3 = 0;<br \/>\nSum of fourth bits%3 = (1)%3 = 1;<br \/>\nHence number which appears once is 1000<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/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\">#include &lt;stdio.h&gt;<br\/>#define INT_SIZE 32<br\/> <br\/>int getSingle(int arr[], int n)<br\/>{<br\/>    \/\/ Initialize result<br\/>    int result = 0;<br\/> <br\/>    int x, sum;<br\/> <br\/>    \/\/ Iterate through every bit<br\/>    for (int i = 0; i &lt; INT_SIZE; i++)<br\/>    {<br\/>      \/\/ Find sum of set bits at ith position in all<br\/>      \/\/ array elements<br\/>      sum = 0;<br\/>      x = (1 &lt;&lt; i);<br\/>      for (int j=0; j&lt; n; j++ )<br\/>      {<br\/>          if (arr[j] &amp; x)<br\/>            sum++;<br\/>      }<br\/> <br\/>      \/\/ The bits with sum not multiple of 3, are the<br\/>      \/\/ bits of element with single occurrence.<br\/>      if (sum % 3)<br\/>        result |= x;<br\/>    }<br\/> <br\/>    return result;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 2, 2, 3, 7};<br\/>    int n = sizeof(arr) \/ sizeof(arr[0]);<br\/>    printf(&quot;The element with single occurrence is %d &quot;,<br\/>            getSingle(arr, n));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<pre>7<\/pre>\n<div class=\"AdsParent\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Find the element that appears once &#8211; Bit Algorithm &#8211; Given an array where every element occurs three times, except one element which occurs only once. Expected time complexity is O(n) and O(1) extra space.<\/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],"tags":[74856,74858,74859,74855,74853,74860,74854,74857],"class_list":["post-25711","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","tag-find-non-repeating-element-in-an-array","tag-find-non-repeating-element-in-an-array-java","tag-find-the-common-elements-of-2-int-arrays","tag-find-the-element-that-appears-once-in-a-sorted-array","tag-find-the-element-that-appears-once-java","tag-find-the-element-that-appears-once-leetcode","tag-given-an-array-of-integers-every-element-appears-twice-except-for-one","tag-there-is-an-array-with-every-element-repeated-twice-except-one-find-that-element-in-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25711","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=25711"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25711\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}