Find the only non-repeating element in a given array
Last Updated :
04 Nov, 2023
Given an array A[] consisting of N (1 ? N ? 105) positive integers, the task is to find the only array element with a single occurrence.
Note: It is guaranteed that only one such element exists in the array.
Examples:
Input: A[] = {1, 1, 2, 3, 3}
Output: 2
Explanation:
Distinct array elements are {1, 2, 3}.
Frequency of these elements are {2, 1, 2} respectively.
Input : A[] = {1, 1, 1, 2, 2, 3, 5, 3, 4, 4}
Output : 5
Approach: Follow the steps below to solve the problem
- Traverse the array
- Use an Unordered Map to store the frequency of array elements.
- Traverse the Map and find the element with frequency 1 and print that element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CalcUnique( int A[], int N)
{
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++) {
freq[A[i]]++;
}
for ( int i = 0; i < N; i++) {
if (freq[A[i]] == 1) {
cout << A[i];
return ;
}
}
}
int main()
{
int A[] = { 1, 1, 2, 3, 3 };
int N = sizeof (A) / sizeof (A[0]);
CalcUnique(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void CalcUnique( int A[], int N)
{
HashMap<Integer,Integer> freq = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < N; i++)
{
if (freq.containsKey(A[i]))
{
freq.put(A[i], freq.get(A[i]) + 1 );
}
else
{
freq.put(A[i], 1 );
}
}
for ( int i = 0 ; i < N; i++)
{
if (freq.containsKey(A[i])&&freq.get(A[i]) == 1 )
{
System.out.print(A[i]);
return ;
}
}
}
public static void main(String[] args)
{
int A[] = { 1 , 1 , 2 , 3 , 3 };
int N = A.length;
CalcUnique(A, N);
}
}
|
Python3
from collections import defaultdict
def CalcUnique(A, N):
freq = defaultdict( int )
for i in range (N):
freq[A[i]] + = 1
for i in range (N):
if (freq[A[i]] = = 1 ):
print (A[i])
return
if __name__ = = "__main__" :
A = [ 1 , 1 , 2 , 3 , 3 ]
N = len (A)
CalcUnique(A, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void CalcUnique( int []A, int N)
{
Dictionary< int , int > freq = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (freq.ContainsKey(A[i]))
{
freq[A[i]] = freq[A[i]] + 1;
}
else
{
freq.Add(A[i], 1);
}
}
for ( int i = 0; i < N; i++)
{
if (freq.ContainsKey(A[i]) && freq[A[i]] == 1)
{
Console.Write(A[i]);
return ;
}
}
}
public static void Main(String[] args)
{
int []A = { 1, 1, 2, 3, 3 };
int N = A.Length;
CalcUnique(A, N);
}
}
|
Javascript
<script>
function CalcUnique(A, N)
{
var freq = new Map();
for ( var i = 0; i < N; i++) {
if (freq.has(A[i]))
{
freq.set(A[i], freq.get(A[i])+1);
}
else
{
freq.set(A[i],1);
}
}
for ( var i = 0; i < N; i++) {
if (freq.get(A[i]) == 1) {
document.write( A[i]);
return ;
}
}
}
var A = [1, 1, 2, 3, 3 ];
var N = A.length;
CalcUnique(A, N);
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(N)
Another Approach: Using Built-in Functions:
- Calculate the frequencies using Built-In function.
- Traverse the array and find the element with frequency 1 and print it.
Below is the implementation:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
void CalcUnique( int A[], int N)
{
unordered_map< int , int > freq;
for ( int i = 0; i < N; i++)
{
int count = 1;
if (freq.count(A[i])) {
count = freq[A[i]];
count++;
}
freq[A[i]] = count;
}
for ( int i = 0; i < N; i++)
{
if (freq[A[i]] == 1) {
cout << A[i] << endl;
return ;
}
}
}
int main()
{
int A[] = { 1, 1, 2, 3, 3 };
int N = sizeof (A) / sizeof (A[0]);
CalcUnique(A, N);
return 0;
}
|
Java
import java.util.Map;
import java.util.HashMap;
class Main{
public static void CalcUnique( int A[], int N)
{
Map<Integer, Integer> freq = new HashMap<Integer, Integer>();
for ( int i= 0 ; i<N; i++)
{
int count = 1 ;
if (freq.containsKey(A[i]))
{
count = freq.get(A[i]);
count++;
}
freq.put(A[i], count);
}
for ( int i= 0 ; i<N; i++)
{
if (freq.get(A[i]) == 1 )
{
System.out.println(A[i]);
return ;
}
}
}
public static void main(String args[])
{
int A[] = { 1 , 1 , 2 , 3 , 3 };
int N = A.length;
CalcUnique(A, N);
}}
|
Python3
from collections import Counter
def CalcUnique(A, N):
freq = Counter(A)
for i in A:
if (freq[i] = = 1 ):
print (i)
return
if __name__ = = "__main__" :
A = [ 1 , 1 , 2 , 3 , 3 ]
N = len (A)
CalcUnique(A, N)
|
C#
using System;
using System.Collections.Generic;
public class MainClass {
static void CalcUnique( int [] A, int N)
{
Dictionary< int , int > freq
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (freq.ContainsKey(A[i])) {
freq[A[i]]++;
}
else {
freq[A[i]] = 1;
}
}
for ( int i = 0; i < N; i++) {
if (freq[A[i]] == 1) {
Console.WriteLine(A[i]);
return ;
}
}
}
public static void Main()
{
int [] A = { 1, 1, 2, 3, 3 };
int N = A.Length;
CalcUnique(A, N);
}
}
|
Javascript
<script>
function CalcUnique(A) {
let freq = A.reduce((acc, curr) => {
if (acc[curr]) {
acc[curr]++;
} else {
acc[curr] = 1;
}
return acc;
}, {});
for (let i = 0; i < A.length; i++) {
if (freq[A[i]] === 1) {
console.log(A[i]);
return ;
}
}
}
const A = [1, 1, 2, 3, 3];
CalcUnique(A);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Another Approach: Using Sorting
The idea is to sort the array and find which element occurs only once by traversing the array.
Steps to implement this Approach-
- Sort the given array
- Then traverse from starting to the end of the array
- Since the array is already sorted that’s why repeating element will be consecutive
- So in that whole array, we will find which element occurred only once. That is our required answer
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void CalcUnique( int arr[], int N)
{
sort(arr,arr+N);
int temp=INT_MIN;
int count=0;
for ( int i=0;i<N;i++){
if (arr[i]==temp){count++;}
else {
if (count==1){
cout<<temp<<endl;
return ;
}
temp=arr[i];
count=1;
}
}
cout<<arr[N-1]<<endl;
}
int main()
{
int arr[] = {1, 1, 2, 3, 3};
int N = sizeof (arr) / sizeof (arr[0]);
CalcUnique(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void CalcUnique( int [] arr, int N) {
Arrays.sort(arr);
int temp=Integer.MIN_VALUE;
int count= 0 ;
for ( int i= 0 ;i<N;i++){
if (arr[i]==temp){
count++;
} else {
if (count== 1 ){
System.out.println(temp);
return ;
}
temp=arr[i];
count= 1 ;
}
}
System.out.println(arr[N- 1 ]);
}
public static void main(String[] args) {
int [] arr = { 1 , 1 , 2 , 3 , 3 };
int N = arr.length;
CalcUnique(arr, N);
}
}
|
Python3
def calc_unique(arr, N):
arr.sort()
temp = None
count = 0
for i in range (N):
if arr[i] = = temp:
count + = 1
else :
if count = = 1 :
print (temp)
return
temp = arr[i]
count = 1
print (arr[N - 1 ])
if __name__ = = "__main__" :
arr = [ 1 , 1 , 2 , 3 , 3 ]
N = len (arr)
calc_unique(arr, N)
|
C#
using System;
using System.Linq;
class Program
{
static void CalcUnique( int [] arr, int N)
{
Array.Sort(arr);
int temp = int .MinValue;
int count = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == temp)
{
count++;
}
else
{
if (count == 1)
{
Console.WriteLine(temp);
return ;
}
temp = arr[i];
count = 1;
}
}
Console.WriteLine(arr[N - 1]);
}
static void Main( string [] args)
{
int [] arr = { 1, 1, 2, 3, 3 };
int N = arr.Length;
CalcUnique(arr, N);
}
}
|
Javascript
function calcUnique(arr) {
arr.sort((a, b) => a - b);
let temp = Number.MIN_SAFE_INTEGER;
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === temp) {
count++;
} else {
if (count === 1) {
console.log(temp);
return ;
}
temp = arr[i];
count = 1;
}
}
console.log(arr[arr.length - 1]);
}
const arr = [1, 1, 2, 3, 3];
calcUnique(arr);
|
Time Complexity: O(NlogN)+O(N)=O(NlogN), O(NlogN) in sorting and O(N) in traversing the array
Auxiliary Space: O(1), because no extra space has been taken
Share your thoughts in the comments
Please Login to comment...