Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find which element occurs exactly once in Python
In Python, data structures such as strings, lists, etc., may contain duplicate values. In some scenarios, we may need to find which element appears only once while all other elements appear multiple times. Let's explore different methods to find which element occurs exactly once in Python.
Using Bitwise XOR
The XOR (Exclusive OR) is a bitwise operator that returns 1 if the corresponding bits of two operands are different, otherwise it returns 0 if they are the same. In Python, we use the ^ operator to perform XOR operations.
The XOR approach works because of these properties:
-
a ^ a = 0(any number XOR with itself is 0) -
a ^ 0 = a(any number XOR with 0 is the number itself) - XOR is commutative and associative
Example
Here we use XOR operation on all elements to find the unique element ?
def find_unique_element(arr):
result = 0
for num in arr:
result ^= num
return result
# Sample input list
numbers = [7, 3, 5, 4, 5, 3, 4]
print("Element that occurs exactly once:", find_unique_element(numbers))
The output of the above code is ?
Element that occurs exactly once: 7
Using collections.Counter
The Counter is a subclass of the collections module in Python that counts the frequency of elements in a container such as list, tuple, or string. It returns a dictionary where elements are keys and their counts are values.
Example
Here we use Counter to count element frequencies and find those with count equal to 1 ?
from collections import Counter
def find_elements_occurring_once(arr):
freq = Counter(arr)
return [element for element, count in freq.items() if count == 1]
# Sample input list
numbers = [1, 3, 4, 7, 8, 7, 3, 4, 8]
print("Elements that occur exactly once:", find_elements_occurring_once(numbers))
The output of the above code is ?
Elements that occur exactly once: [1]
Using Dictionary to Count Frequencies
We can manually count element frequencies using a dictionary and then filter elements that occur exactly once ?
def find_unique_with_dict(arr):
freq = {}
# Count frequencies
for element in arr:
freq[element] = freq.get(element, 0) + 1
# Find elements with count 1
unique_elements = [element for element, count in freq.items() if count == 1]
return unique_elements
# Sample input
data = ['a', 'b', 'c', 'a', 'd', 'b', 'e']
print("Elements occurring exactly once:", find_unique_with_dict(data))
The output of the above code is ?
Elements occurring exactly once: ['c', 'd', 'e']
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Bitwise XOR | O(n) | O(1) | Single unique element, integers only |
| Counter | O(n) | O(n) | Multiple unique elements, any data type |
| Dictionary | O(n) | O(n) | Custom counting logic needed |
Conclusion
Use XOR for finding a single unique integer in constant space. Use Counter or dictionary methods when you need to find all elements that occur exactly once or work with non-numeric data types.
