Find first non-repeating element in a given Array of integers
Last Updated :
11 Sep, 2023
Given an array of integers of size N, the task is to find the first non-repeating element in this array.
Examples:
Input: {-1, 2, -1, 3, 0}
Output: 2
Explanation: The first number that does not repeat is : 2
Input: {9, 4, 9, 6, 7, 4}
Output: 6
Simple Solution is to use two loops. The outer loop picks elements one by one and the inner loop checks if the element is present more than once or not..
Illustration:
Given arr[] = {-1, 2, -1, 3, 0}
For element at i = 0:
- The value of element at index 2 is same, then this can’t be first non-repeating element
For element at i = 1:
- After traversing the array arr[1] is not present is not present in the array except at 1.
Hence, element is index 1 is the first non-repeating element which is 2
Follow the steps below to solve the given problem:
- Loop over the array from the left.
- Check for each element if its presence is present in the array for more than 1 time.
- Use a nested loop to check the presence.
Below is the implementation of the above idea:
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));
}
}
|
JavaScript
<script>
function firstNonRepeating(arr, n) {
for (let i = 0; i < n; i++) {
let j;
for (j = 0; j < n; j++)
if (i != j && arr[i] == arr[j])
break ;
if (j == n)
return arr[i];
}
return -1;
}
let arr = [9, 4, 9, 6, 7, 4];
let n = arr.length;
document.write(firstNonRepeating(arr, n));
</script>
|
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 );
?>
|
Time Complexity: O(n*n), Checking for each element n times
Auxiliary Space: O(1)
Find first non-repeating element in a given Array of integers using Hashing:
This approach is based on the following idea:
- The idea is to store the frequency of every element in the hashmap.
- Then check the first element whose frequency is 1 in the hashmap.
- This can be achieved using hashing
Illustration:
arr[] = {-1, 2, -1, 3, 0}
Frequency map for arr:
- -1 -> 2
- 2 -> 1
- 3 -> 1
- 0 -> 1
Traverse arr[] from left:
At i = 0:
- Frequency of arr[0] is 2, therefore it can’t be first non-repeating element
At i = 1:
- Frequency of arr[1] is 1, therefore it will be the first non-repeating element.
Hence, 2 is the first non-repeating element.
Follow the steps below to solve the given problem:
- Traverse array and insert elements and their counts in the hash table.
- Traverse array again and print the first element with a count equal to 1.
Below is the implementation of the above idea:
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));
}
}
|
Javascript
<script>
function firstNonRepeating(arr , n)
{
const m = new Map();
for ( var i = 0; i < n; i++) {
if (m.has(arr[i])) {
m.set(arr[i], m.get(arr[i]) + 1);
}
else {
m.set(arr[i], 1);
}
}
for ( var i = 0; i < n; i++)
if (m.get(arr[i]) == 1)
return arr[i];
return -1;
}
var arr = [ 9, 4, 9, 6, 7, 4 ];
var n = arr.length;
document.write(firstNonRepeating(arr, n));
</script>
|
Time Complexity: O(n), Traverse over the array to map the frequency and again traverse over the array to check for frequency.
Auxiliary Space: O(n), Create a hash table for storing frequency