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.
Examples:

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.

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.

Run a loop for all elements in array. At the end of every iteration, maintain following two values.

ones: The bits that have appeared 1st time or 4th time or 7th time .. etc.

twos: The bits that have appeared 2nd time or 5th time or 8th time .. etc.

Finally, we return the value of ‘ones’

[ad type=”banner”]

How to maintain the values of ‘ones’ and ‘twos’?
‘ones’ and ‘twos’ are initialized as 0. For every new element in array, find out the common set bits in the new element and previous value of ‘ones’. These common set bits are actually the bits that should be added to ‘twos’. So do bitwise OR of the common set bits with ‘twos’. ‘twos’ also gets some extra bits that appear third time. These extra bits are removed later.
Update ‘ones’ by doing XOR of new element with previous value of ‘ones’. There may be some bits which appear 3rd time. These extra bits are also removed later.

Both ‘ones’ and ‘twos’ contain those extra bits which appear 3rd time. Remove these extra bits by finding out common set bits in ‘ones’ and ‘twos’.

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20getSingle(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20ones%20%3D%200%2C%20twos%20%3D%200%20%3B%0A%20%0A%20%20%20%20int%20common_bit_mask%3B%0A%20%0A%20%20%20%20%2F%2F%20Let%20us%20take%20the%20example%20of%20%7B3%2C%203%2C%202%2C%203%7D%20to%20understand%20this%0A%20%20%20%20for(%20int%20i%3D0%3B%20i%3C%20n%3B%20i%2B%2B%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20The%20expression%20%22one%20%26%20arr%5Bi%5D%22%20gives%20the%20bits%20that%20are%0A%20%20%20%20%20%20%20%20%20%20%20there%20in%20both%20’ones’%20and%20new%20element%20from%20arr%5B%5D.%20%20We%0A%20%20%20%20%20%20%20%20%20%20%20add%20these%20bits%20to%20’twos’%20using%20bitwise%20OR%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20Value%20of%20’twos’%20will%20be%20set%20as%200%2C%203%2C%203%20and%201%20after%201st%2C%0A%20%20%20%20%20%20%20%20%20%20%202nd%2C%203rd%20and%204th%20iterations%20respectively%20*%2F%0A%20%20%20%20%20%20%20%20twos%20%20%3D%20twos%20%7C%20(ones%20%26%20arr%5Bi%5D)%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20XOR%20the%20new%20bits%20with%20previous%20’ones’%20to%20get%20all%20bits%0A%20%20%20%20%20%20%20%20%20%20%20appearing%20odd%20number%20of%20times%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20Value%20of%20’ones’%20will%20be%20set%20as%203%2C%200%2C%202%20and%203%20after%201st%2C%0A%20%20%20%20%20%20%20%20%20%20%202nd%2C%203rd%20and%204th%20iterations%20respectively%20*%2F%0A%20%20%20%20%20%20%20%20ones%20%20%3D%20ones%20%5E%20arr%5Bi%5D%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20The%20common%20bits%20are%20those%20bits%20which%20appear%20third%20time%0A%20%20%20%20%20%20%20%20%20%20%20So%20these%20bits%20should%20not%20be%20there%20in%20both%20’ones’%20and%20’twos’.%0A%20%20%20%20%20%20%20%20%20%20%20common_bit_mask%20contains%20all%20these%20bits%20as%200%2C%20so%20that%20the%20bits%20can%20%0A%20%20%20%20%20%20%20%20%20%20%20be%20removed%20from%20’ones’%20and%20’twos’%20%20%20%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20Value%20of%20’common_bit_mask’%20will%20be%20set%20as%2000%2C%2000%2C%2001%20and%2010%0A%20%20%20%20%20%20%20%20%20%20%20after%201st%2C%202nd%2C%203rd%20and%204th%20iterations%20respectively%20*%2F%0A%20%20%20%20%20%20%20%20common_bit_mask%20%3D%20~(ones%20%26%20twos)%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Remove%20common%20bits%20(the%20bits%20that%20appear%20third%20time)%20from%20’ones’%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20Value%20of%20’ones’%20will%20be%20set%20as%203%2C%200%2C%200%20and%202%20after%201st%2C%0A%20%20%20%20%20%20%20%20%20%20%202nd%2C%203rd%20and%204th%20iterations%20respectively%20*%2F%0A%20%20%20%20%20%20%20%20ones%20%26%3D%20common_bit_mask%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Remove%20common%20bits%20(the%20bits%20that%20appear%20third%20time)%20from%20’twos’%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20Value%20of%20’twos’%20will%20be%20set%20as%200%2C%203%2C%201%20and%200%20after%201st%2C%0A%20%20%20%20%20%20%20%20%20%20%202nd%2C%203rd%20and%204th%20itearations%20respectively%20*%2F%0A%20%20%20%20%20%20%20%20twos%20%26%3D%20common_bit_mask%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20uncomment%20this%20code%20to%20see%20intermediate%20values%0A%20%20%20%20%20%20%20%20%2F%2Fprintf%20(%22%20%25d%20%25d%20%5Cn%22%2C%20ones%2C%20twos)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20ones%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B3%2C%203%2C%202%2C%203%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%20%2F%20sizeof(arr%5B0%5D)%3B%0A%20%20%20%20printf(%22The%20element%20with%20single%20occurrence%20is%20%25d%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20getSingle(arr%2C%20n))%3B%0A%20%20%20%20return%200%3B%0A%7D%0ARun%20on%20IDE%0AOutput%3A%0A%0A” message=”C” highlight=”” provider=”manual”/]

Output:

2

Time Complexity: O(n)
Auxiliary Space: O(1)

[ad type=”banner”]

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.
Let us consider the example array {5, 5, 5, 8}. The 101, 101, 101, 1000
Sum of first bits%3 = (1 + 1 + 1 + 0)%3 = 0;
Sum of second bits%3 = (0 + 0 + 0 + 0)%0 = 0;
Sum of third bits%3 = (1 + 1 + 1 + 0)%3 = 0;
Sum of fourth bits%3 = (1)%3 = 1;
Hence number which appears once is 1000

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23define%20INT_SIZE%2032%0A%20%0Aint%20getSingle(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20result%0A%20%20%20%20int%20result%20%3D%200%3B%0A%20%0A%20%20%20%20int%20x%2C%20sum%3B%0A%20%0A%20%20%20%20%2F%2F%20Iterate%20through%20every%20bit%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20INT_SIZE%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20Find%20sum%20of%20set%20bits%20at%20ith%20position%20in%20all%0A%20%20%20%20%20%20%2F%2F%20array%20elements%0A%20%20%20%20%20%20sum%20%3D%200%3B%0A%20%20%20%20%20%20x%20%3D%20(1%20%3C%3C%20i)%3B%0A%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3C%20n%3B%20j%2B%2B%20)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%26%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20sum%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%2F%2F%20The%20bits%20with%20sum%20not%20multiple%20of%203%2C%20are%20the%0A%20%20%20%20%20%20%2F%2F%20bits%20of%20element%20with%20single%20occurrence.%0A%20%20%20%20%20%20if%20(sum%20%25%203)%0A%20%20%20%20%20%20%20%20result%20%7C%3D%20x%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20result%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B12%2C%201%2C%2012%2C%203%2C%2012%2C%201%2C%201%2C%202%2C%203%2C%202%2C%202%2C%203%2C%207%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%20%2F%20sizeof(arr%5B0%5D)%3B%0A%20%20%20%20printf(%22The%20element%20with%20single%20occurrence%20is%20%25d%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20getSingle(arr%2C%20n))%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]
7

Categorized in: