How to Find the Element that Appears Once in an Array

Search for a command to run...

No comments yet. Be the first to comment.
Finding the maximum number of consecutive 1's in a binary array is a common problem that can be efficiently solved with a linear time algorithm. In this article, we will discuss an optimal approach to solve this problem. Solution: Optimal Approach Th...
Finding the maximum number of consecutive 1's in a binary array is a common problem that can be efficiently solved with a linear time algorithm. In this article, we will discuss an optimal approach to solve this problem. Solution: Optimal Approach Th...

In an array containing numbers from 1 to N, where one number is missing, the goal is to find the missing number. Here, we'll discuss four different methods to achieve this, ranging from brute force to optimal approaches. Solution 1: Brute Force Appro...

Finding the intersection of two sorted arrays is a common problem in coding interviews and programming challenges. In this article, we'll discuss two approaches to solve this problem: one using a brute force approach and another using two-pointers te...

Finding the union of two sorted arrays involves combining the elements of both arrays without duplicates. This problem can be solved using different approaches, each with its own time and space complexity. In this article, we'll discuss two approache...

Finding the element that appears only once in an array where all other elements appear twice is a common problem in coding interviews and programming challenges. In this article, we'll discuss four approaches to solve this problem: one using a brute force approach, another using hashing, a third using a map, and the optimal approach using XOR.
This method involves using a nested loop to find the element that appears only once.
Implementation:
// Solution-1: Brute Force Approach (using Nested Loop)
// Time Complexity: O(n*n)
// Space Complexity: O(1)
int getSingleElement(vector<int> &arr, int n)
{
// Loop for selecting elements
for (int i = 0; i < n; i++)
{
// Selected element
int num = arr[i];
int counter = 0;
// Find the occurrence using Linear Search
for (int j = 0; j < n; j++)
{
if (arr[j] == num)
{
counter++;
}
}
// If the occurrence is 1, return that element
if (counter == 1)
{
return num;
}
}
// In case no element appears once
return -1;
}
Logic:
Nested Loop:
Use an outer loop to iterate through each element of the array.
Use an inner loop to count the occurrences of the selected element.
Check for Single Occurrence:
1, return that element.Time Complexity: O(n*n)
Space Complexity: O(1)
Example:
Input: arr = [4, 3, 2, 4, 1, 3, 2]
Output: 1
Explanation: The element 1 appears only once in the array.
This method uses hashing to find the element that appears only once.
Implementation:
// Solution-2: Better Approach 1 (using Hashing)
// Time Complexity: O(3n) ~ O(n)
// Space Complexity: O(maxElement+1)
int getSingleElement(vector<int> &arr, int n)
{
// Find the maximum element
int maxElement = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i] > maxElement)
{
maxElement = arr[i];
}
}
// Hash Array of size maxElement + 1
vector<int> hash(maxElement + 1, 0);
// Hash the array
for (int i = 0; i < n; i++)
{
hash[arr[i]]++;
}
// Find the single element and return that
for (int i = 0; i < n; i++)
{
if (hash[arr[i]] == 1)
{
return arr[i];
}
}
// In case no element appears once
return -1;
}
Logic:
Find Maximum Element:
Hash the Array:
maxElement + 1 and count the occurrences of each element. maxElement is the maximum element in the array.Check for Single Occurrence:
1.Time Complexity: O(3n) ~ O(n)
Space Complexity: O(maxElement+1)
maxElement is the maximum element in the array.Example:
Input: arr = [4, 3, 2, 4, 1, 3, 2]
Output: 1
Explanation: The element 1 appears only once in the array.
This method uses a map to find the element that appears only once.
Implementation:
// Solution-3: Better Approach 2 (using Map for Hashing)
// Time Complexity: O(n * log M) + O(M)
// Space Complexity: O(M)
int getSingleElement(vector<int> &arr, int n)
{
// Declare the Map
map<int, int> track;
// Hash the array
for (int i = 0; i < n; i++)
{
track[arr[i]]++;
}
// Find the single element and return that
for (auto it : track)
{
if (it.second == 1)
{
return it.first;
}
}
// In case no element appears once
return -1;
}
Logic:
Hash the Array:
Check for Single Occurrence:
1.Time Complexity: O(n * log M) + O(M)
Space Complexity: O(M)
Example:
Input: arr = [4, 3, 2, 4, 1, 3, 2]
Output: 1
Explanation: The element 1 appears only once in the array.
This method uses XOR to find the element that appears only once.
Implementation:
// Solution-4: Optimal Approach (using XOR)
// Time Complexity: O(n)
// Space Complexity: O(1)
int getSingleElement(vector<int> &arr, int n)
{
int appearsOnce = 0;
// XOR all the elements
for (int i = 0; i < n; i++)
{
appearsOnce ^= arr[i];
}
return appearsOnce;
}
Logic:
XOR All Elements:
XOR all the elements of the array. The elements appearing twice will cancel out, leaving the element that appears once.
This approach leverages the property a ^ a = 0 and a ^ 0 = a.
Time Complexity: O(n)
Space Complexity: O(1)
Example:
Input: arr = [4, 3, 2, 4, 1, 3, 2]
Output: 1
Explanation: The element 1 appears only once in the array.
Brute Force Approach:
Pros: Simple and straightforward.
Cons: Inefficient for large arrays due to O(n*n) time complexity.
Better Approach (using Hashing):
Pros: More efficient than brute force with O(3n) ~ O(n) time complexity.
Cons: Requires additional space for the hash array.
Better Approach (using Map for Hashing):
Pros: Efficient with O(n * log M) + O(M) time complexity.
Cons: Requires additional space for the map for hashing.
Optimal Approach (using XOR):
Pros: Highly efficient with O(n) time complexity and no additional space usage.
Cons: Slightly more complex to understand but very efficient.
Empty Array: Returns -1 as there are no elements to check.
No Single Element: Returns -1 if no element appears exactly once.
All Elements are the Same: Returns -1 if no unique element exists.
Efficiency: The XOR technique is the most time-efficient and space-efficient, making it preferable for large arrays.
Practicality: The choice of approach depends on the specific constraints and requirements of the problem, such as array size and available memory.
Finding the element that appears once in an array where all other elements appear twice can be efficiently achieved using various approaches. The optimal choice depends on the specific constraints and requirements of the problem, with the XOR method being the most efficient in terms of both time and space.