Find the first non-repeating element in a given array of integers.
Examples:
Input : -1 2 -1 3 2
Output : 3
Explanation : The first number that does not
repeat is : 3
Input : 9 4 9 6 7 4
Output : 6
A Simple Solution is to use two loops. The outer loop picks elements one by one and inner loop checks if the element is present more than once or not.
C++
#include <bits/stdc++.h>
using namespace std;
int firstNonRepeating( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
int j;
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j])
break ;
if (j == n)
return arr[i];
}
return -1;
}
int main()
{
int arr[] = { 9, 4, 9, 6, 7, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << firstNonRepeating(arr, n);
return 0;
}
|
Java
class GFG {
static int firstNonRepeating( int arr[], int n)
{
for ( int i = 0 ; i < n; i++) {
int j;
for (j = 0 ; j < n; j++)
if (i != j && arr[i] == arr[j])
break ;
if (j == n)
return arr[i];
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 9 , 4 , 9 , 6 , 7 , 4 };
int n = arr.length;
System.out.print(firstNonRepeating(arr, n));
}
}
|
Python3
def firstNonRepeating(arr, n):
for i in range (n):
j = 0
while (j < n):
if (i ! = j and arr[i] = = arr[j]):
break
j + = 1
if (j = = n):
return arr[i]
return - 1
arr = [ 9 , 4 , 9 , 6 , 7 , 4 ]
n = len (arr)
print (firstNonRepeating(arr, n))
|
C#
using System;
class GFG {
static int firstNonRepeating( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
int j;
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j])
break ;
if (j == n)
return arr[i];
}
return -1;
}
public static void Main()
{
int [] arr = { 9, 4, 9, 6, 7, 4 };
int n = arr.Length;
Console.Write(firstNonRepeating(arr, n));
}
}
|
PHP
<?php
function firstNonRepeating( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$j ;
for ( $j = 0; $j < $n ; $j ++)
if ( $i != $j && $arr [ $i ] == $arr [ $j ])
break ;
if ( $j == $n )
return $arr [ $i ];
}
return -1;
}
$arr = array (9, 4, 9, 6, 7, 4);
$n = sizeof( $arr ) ;
echo firstNonRepeating( $arr , $n );
?>
|
An Efficient Solution is to use hashing.
1) Traverse array and insert elements and their counts in hash table.
2) Traverse array again and print first element with count equals to 1.
C++
#include <bits/stdc++.h>
using namespace std;
int firstNonRepeating( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
for ( int i = 0; i < n; i++)
if (mp[arr[i]] == 1)
return arr[i];
return -1;
}
int main()
{
int arr[] = { 9, 4, 9, 6, 7, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << firstNonRepeating(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int firstNonRepeating( int arr[], int n)
{
Map<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i], m.get(arr[i]) + 1 );
}
else {
m.put(arr[i], 1 );
}
}
for ( int i = 0 ; i < n; i++)
if (m.get(arr[i]) == 1 )
return arr[i];
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 9 , 4 , 9 , 6 , 7 , 4 };
int n = arr.length;
System.out.println(firstNonRepeating(arr, n));
}
}
|
Python3
from collections import defaultdict
def firstNonRepeating(arr, n):
mp = defaultdict( lambda : 0 )
for i in range (n):
mp[arr[i]] + = 1
for i in range (n):
if mp[arr[i]] = = 1 :
return arr[i]
return - 1
arr = [ 9 , 4 , 9 , 6 , 7 , 4 ]
n = len (arr)
print (firstNonRepeating(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int firstNonRepeating( int [] arr, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (m.ContainsKey(arr[i])) {
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else {
m.Add(arr[i], 1);
}
}
for ( int i = 0; i < n; i++)
if (m[arr[i]] == 1)
return arr[i];
return -1;
}
public static void Main(String[] args)
{
int [] arr = { 9, 4, 9, 6, 7, 4 };
int n = arr.Length;
Console.WriteLine(firstNonRepeating(arr, n));
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Further Optimization: If array has many duplicates, we can also store index in hash table, using a hash table where value is a pair. Now we only need to traverse keys in hash table (not complete array) to find first non repeating.
Printing all non-repeating elements:
C++
#include <bits/stdc++.h>
using namespace std;
void firstNonRepeating( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
for ( auto x : mp)
if (x.second == 1)
cout << x.first << " " ;
}
int main()
{
int arr[] = { 9, 4, 9, 6, 7, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
firstNonRepeating(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void firstNonRepeating( int arr[], int n)
{
Map<Integer, Integer> m = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
if (m.containsKey(arr[i])) {
m.put(arr[i], m.get(arr[i]) + 1 );
}
else {
m.put(arr[i], 1 );
}
}
for (Map.Entry<Integer, Integer> x : m.entrySet())
if (x.getValue() == 1 )
System.out.print(x.getKey() + " " );
}
public static void main(String[] args)
{
int arr[] = { 9 , 4 , 9 , 6 , 7 , 4 };
int n = arr.length;
firstNonRepeating(arr, n);
}
}
|
Python3
def firstNonRepeating(arr, n):
mp = {}
for i in range (n):
if arr[i] not in mp:
mp[arr[i]] = 0
mp[arr[i]] + = 1
for x in mp:
if (mp[x] = = 1 ):
print (x,end = " " )
arr = [ 9 , 4 , 9 , 6 , 7 , 4 ]
n = len (arr)
firstNonRepeating(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void firstNonRepeating( int [] arr, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (m.ContainsKey(arr[i])) {
var val = m[arr[i]];
m.Remove(arr[i]);
m.Add(arr[i], val + 1);
}
else {
m.Add(arr[i], 1);
}
}
foreach (KeyValuePair< int , int > x in m)
{
if (x.Value == 1) {
Console.Write(x.Key + " " );
}
}
}
public static void Main(String[] args)
{
int [] arr = { 9, 4, 9, 6, 7, 4 };
int n = arr.Length;
firstNonRepeating(arr, n);
}
}
|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.