Python Program for Find the element that appears once Last Updated : 22 Dec, 2021 Improve Improve Like Article Like Save Share Report 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 : Input: arr[] = {12, 1, 12, 3, 12, 1, 1, 2, 3, 3} Output: 2 In the given array all element appear three times except 2 which appears once. Input: arr[] = {10, 20, 10, 30, 10, 30, 30} Output: 20 In the given array all element appear three times except 20 which appears once. Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Python3 # Python3 code to find the element that # appears once def getSingle(arr, n): ones = 0 twos = 0 for i in range(n): # one & arr[i]" gives the bits that # are there in both \'ones\' and new # element from arr[]. We add these # bits to \'twos\' using bitwise OR twos = twos | (ones & arr[i]) # one & arr[i]" gives the bits that # are there in both \'ones\' and new # element from arr[]. We add these # bits to \'twos\' using bitwise OR ones = ones ^ arr[i] # The common bits are those bits # which appear third time. So these # bits should not be there in both # \'ones\' and \'twos\'. common_bit_mask # contains all these bits as 0, so # that the bits can be removed from # \'ones\' and \'twos\' common_bit_mask = ~(ones & twos) # Remove common bits (the bits that # appear third time) from \'ones\' ones &= common_bit_mask # Remove common bits (the bits that # appear third time) from \'twos\' twos &= common_bit_mask return ones # driver code arr = [3, 3, 2, 3] n = len(arr) print("The element with single occurrence is ", getSingle(arr, n)) # This code is contributed by "Abhishek Sharma 44" Please refer complete article on Find the element that appears once for more details! Like Article Suggest improvement Previous find_element_by_css_selector() driver method - Selenium Python Next Find frequencies of elements of an array present in another array Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Python | Replace multiple characters at once Python | Iterating two lists at once Python - Replace Different characters in String at Once Python | Append multiple lists at once Python program to get the next unrevealed element for the given element Python Program to find the cube of each list element Python Program to find the Next Nearest element in a Matrix Python Program to Find maximum element of each row in a matrix Python Program to Find a triplet such that sum of two equals to third element Python Program to Find Largest Element in an Array Like GeeksforGeeks Article Tags : Python Programs