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
Find the number that appears once, and the other numbers twice in Python
Finding a unique number in a list that appears only once while all other numbers appear twice is a common programming problem. Python provides several approaches to solve this efficiently.
The most efficient solution uses the XOR operator, which solves the problem in constant space and linear time complexity. In this article, we'll explore various methods to find the number that appears once while all others appear twice.
Problem Understanding
Given a list of numbers where every number appears exactly twice except one number that appears only once, we need to find that unique number ?
Input & Output Examples
# Example 1 nums1 = [4, 3, 2, 4, 3] # Expected output: 2 # Example 2 nums2 = [1, 1, 2, 2, 3] # Expected output: 3
Using XOR Operator (Most Efficient)
This approach leverages the XOR operator's properties: N ^ N = 0 and 0 ^ N = N. When we XOR all numbers, duplicates cancel out, leaving only the unique number ?
def find_unique_xor(nums):
unique_number = 0
for num in nums:
unique_number ^= num
return unique_number
# Test with examples
nums1 = [4, 3, 2, 4, 3]
result1 = find_unique_xor(nums1)
print(f"The number that appears once: {result1}")
nums2 = [1, 1, 2, 2, 3]
result2 = find_unique_xor(nums2)
print(f"The number that appears once: {result2}")
The number that appears once: 2 The number that appears once: 3
Time Complexity: O(n) | Space Complexity: O(1)
Using Dictionary (Hash Map)
Count the frequency of each number using a dictionary, then find the number with frequency 1 ?
def find_unique_dict(nums):
frequency = {}
# Count frequencies
for num in nums:
frequency[num] = frequency.get(num, 0) + 1
# Find number with frequency 1
for num, count in frequency.items():
if count == 1:
return num
# Test the function
nums = [1, 1, 2, 2, 3]
result = find_unique_dict(nums)
print(f"The number that appears once: {result}")
The number that appears once: 3
Time Complexity: O(n) | Space Complexity: O(n)
Using Set Mathematics
Use the mathematical property: 2 × sum(unique_elements) - sum(all_elements) = unique_number ?
def find_unique_set(nums):
return 2 * sum(set(nums)) - sum(nums)
# Test the function
nums = [4, 3, 2, 4, 3]
result = find_unique_set(nums)
print(f"The number that appears once: {result}")
The number that appears once: 2
Time Complexity: O(n) | Space Complexity: O(n)
Using List Count Method
Simple approach using Python's builtin count() method ?
def find_unique_count(nums):
for num in nums:
if nums.count(num) == 1:
return num
# Test the function
nums = [4, 3, 2, 4, 3]
result = find_unique_count(nums)
print(f"The number that appears once: {result}")
The number that appears once: 2
Time Complexity: O(n²) | Space Complexity: O(1)
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| XOR Operator | O(n) | O(1) | Most efficient solution |
| Dictionary | O(n) | O(n) | When you need frequency counts |
| Set Mathematics | O(n) | O(n) | Elegant mathematical approach |
| Count Method | O(n²) | O(1) | Simple implementation |
Conclusion
The XOR operator approach is the most efficient solution with O(n) time and O(1) space complexity. Use the dictionary method when you need to track frequencies, and avoid the count method for large datasets due to its O(n²) complexity.
