Given an array of nums of n integers. Every integer in the array appears twice except one integer. Find the number that appeared once in the array.
Input : nums = [1, 2, 2, 4, 3, 1, 4]
Output : 3
Explanation : The integer 3 has appeared only once.
Input : nums = [5]
Output : 5
Explanation : The integer 5 has appeared only once.
Input : nums = [1, 3, 10, 3, 5, 1, 5]
The brute force way to solve this will be to utilize a frequency counting approach. By keeping track of the frequency of each element in the array, the element that appears only once can be easily identified.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to get the single
number in the given array */
int singleNumber(vector<int>& nums){
/* Map to store the elements
and their frequencies */
unordered_map <int, int> mpp;
// Iterate on the array
for(int i=0; i < nums.size(); i++) {
mpp[nums[i]]++; //Update the map
}
//Iterate on the map
for(auto it : mpp) {
// If frequency is 1
if(it.second == 1) {
// Return the element
return it.first;
}
}
/* Return -1, if there is no
number having frequency 1 */
return -1;
}
};
int main() {
vector<int> nums = {1, 2, 2, 4, 3, 1, 4};
/* Creating an instance of
Solution class */
Solution sol;
/* Function call to get the single
number in the given array */
int ans = sol.singleNumber(nums);
cout << "The single number in given array is: " << ans;
return 0;
}
import java.util.HashMap;
class Solution {
/* Function to get the single
number in the given array */
public int singleNumber(int[] nums) {
/* Map to store the elements
and their frequencies */
HashMap<Integer, Integer> mpp = new HashMap<>();
// Iterate on the array
for (int num : nums) {
mpp.put(num, mpp.getOrDefault(num, 0) + 1); //Update the map
}
//Iterate on the map
for (int key : mpp.keySet()) {
// If frequency is 1
if (mpp.get(key) == 1) {
// Return the element
return key;
}
}
/* Return -1, if there is no
number having frequency 1 */
return -1;
}
public static void main(String[] args) {
int[] nums = {1, 2, 2, 4, 3, 1, 4};
/* Creating an instance of
Solution class */
Solution sol = new Solution();
/* Function call to get the single
number in the given array */
int ans = sol.singleNumber(nums);
System.out.println("The single number in given array is: " + ans);
}
}
class Solution:
# Function to get the single
# number in the given array
def singleNumber(self, nums):
# Map to store the elements
# and their frequencies
mpp = {}
# Iterate on the array
for num in nums:
if num in mpp:
mpp[num] += 1 #Update the map
else:
mpp[num] = 1 #Update the map
# Iterate on the map
for key, value in mpp.items():
# If frequency is 1
if value == 1:
# Return the element
return key
# Return -1, if there is no
# number having frequency 1
return -1
# Example usage
nums = [1, 2, 2, 4, 3, 1, 4]
# Creating an instance of
# Solution class
sol = Solution()
# Function call to get the single
# number in the given array
ans = sol.singleNumber(nums)
print("The single number in given array is:", ans)
class Solution {
/* Function to get the single
number in the given array */
singleNumber(nums) {
/* Map to store the elements
and their frequencies */
let mpp = new Map();
// Iterate on the array
nums.forEach(num => {
mpp.set(num, (mpp.get(num) || 0) + 1); //Update the map
});
// Iterate on the map
for (let [key, value] of mpp) {
// If frequency is 1
if (value === 1) {
// Return the element
return key;
}
}
/* Return -1, if there is no
number having frequency 1 */
return -1;
}
}
// Example usage
let nums = [1, 2, 2, 4, 3, 1, 4];
/* Creating an instance of
Solution class */
let sol = new Solution();
/* Function call to get the single
number in the given array */
let ans = sol.singleNumber(nums);
console.log("The single number in given array is:", ans);
Time Complexity: O(N) (where N is the size of the array) –
Space Complexity: O(N) – Using a hashmap data structure and in the worst-case (when all elements in the array are unique), it will store N key-value pairs.
The problem can be efficiently solved using the properties of the XOR bitwise operator. The key properties of XOR are:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to get the single
number in the given array */
int singleNumber(vector<int>& nums){
/* Variable to store XOR
of all numbers in array */
int XOR = 0;
/* Iterate on the array to
find XOR of all elements */
for(int i = 0; i < nums.size(); i++) {
XOR ^= nums[i];
}
// XOR stores the required answer
return XOR;
}
};
int main() {
vector<int> nums = {1, 2, 2, 4, 3, 1, 4};
/* Creating an instance of
Solution class */
Solution sol;
/* Function call to get the single
number in the given array */
int ans = sol.singleNumber(nums);
cout << "The single number in given array is: " << ans;
return 0;
}
import java.util.*;
class Solution {
/* Function to get the single
number in the given array */
public int singleNumber(int[] nums) {
/* Variable to store XOR
of all numbers in array */
int XOR = 0;
/* Iterate on the array to
find XOR of all elements */
for (int i = 0; i < nums.length; i++) {
XOR ^= nums[i];
}
// XOR stores the required answer
return XOR;
}
}
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 2, 4, 3, 1, 4};
/* Creating an instance of
Solution class */
Solution sol = new Solution();
/* Function call to get the single
number in the given array */
int ans = sol.singleNumber(nums);
System.out.println("The single number in given array is: " + ans);
}
}
class Solution:
# Function to get the single
# number in the given array
def singleNumber(self, nums):
# Variable to store XOR
# of all numbers in array
XOR = 0
# Iterate on the array to
# find XOR of all elements
for num in nums:
XOR ^= num
# XOR stores the required answer
return XOR
if __name__ == "__main__":
nums = [1, 2, 2, 4, 3, 1, 4]
# Creating an instance of
# Solution class
sol = Solution()
# Function call to get the single
# number in the given array
ans = sol.singleNumber(nums)
print(f"The single number in given array is: {ans}")
class Solution {
/* Function to get the single
number in the given array */
singleNumber(nums) {
/* Variable to store XOR
of all numbers in array */
let XOR = 0;
/* Iterate on the array to
find XOR of all elements */
for (let i = 0; i < nums.length; i++) {
XOR ^= nums[i];
}
// XOR stores the required answer
return XOR;
}
}
const nums = [1, 2, 2, 4, 3, 1, 4];
/* Creating an instance of
Solution class */
const sol = new Solution();
/* Function call to get the single
number in the given array */
const ans = sol.singleNumber(nums);
console.log("The single number in given array is: " + ans);
Time Complexity: O(N) (where N is the size of array) – Traversing through the array once will result in O(N) TC.
Space Complexity: O(1) – Using constant space.
Q: Can I solve this without using XOR?
A: Yes, you can use a hash map to count the frequency of each number and return the one with a count of 1. However, this approach uses O(n) time but also requires O(n) space.
Q: Why does XOR work for this problem?
A: XOR cancels out duplicate numbers because a⊕a=0. When all numbers in the array are XORed, only the unique number remains, as 0⊕unique number=unique number.
Q: How would you extend this problem if the unique number appears k times instead of 1?
A: XOR alone wouldn’t work. Instead, you could use bitwise counting to count the occurrences of each bit across all numbers and find the unique number that appears k times.
Q: How would you modify the solution if there were exactly two unique numbers?
A: XOR all elements to get a⊕b (the XOR of the two unique numbers). Then, isolate a differing bit and use it to partition the numbers into two groups, each containing one unique number.