Non-Repeating Element

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++

// Simple CPP program to find first non-
// repeating element.
#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;
}
  
// Driver code
int main()
{
    int arr[] = { 9, 4, 9, 6, 7, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << firstNonRepeating(arr, n);
    return 0;
}

Java

// Java program to find first non-repeating 
// element.
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;
    }
     
    //Driver code
    public static void main (String[] args)
    {
          
        int arr[] = { 9, 4, 9, 6, 7, 4 };
        int n = arr.length;
          
        System.out.print(firstNonRepeating(arr, n));
    }
}
  
// This code is contributed by Anant Agarwal.

Python3

# Python3 program to find first 
# non-repeating element.
  
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
      
# Driver code
arr = [ 9, 4, 9, 6, 7, 4 ]
n = len(arr)
print(firstNonRepeating(arr, n))
  
# This code is contributed by Anant Agarwal.

C#

// C# program to find first non-
// repeating element.
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;
    }
      
    // Driver code
    public static void Main ()
    {
        int []arr = { 9, 4, 9, 6, 7, 4 };
        int n = arr.Length;
        Console.Write(firstNonRepeating(arr, n));
    }
}
// This code is contributed by Anant Agarwal.

PHP

<?php
// Simple PHP program to find first non-
// repeating element.
  
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;
}
  
    // Driver code
    $arr = array(9, 4, 9, 6, 7, 4);
    $n = sizeof($arr) ;
    echo firstNonRepeating($arr, $n);
      
// This code is contributed by ajit
?>

Output:

6

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.

// Efficient CPP program to find first non-
// repeating element.
#include <bits/stdc++.h>
using namespace std;
  
int firstNonRepeating(int arr[], int n)
{
    // Insert all array elements in hash
    // table
    unordered_map<int, int> mp;
    for (int i = 0; i < n; i++) 
       mp[arr[i]]++;
  
    // Traverse array again and return
    // first element with count 1.
    for (int i = 0; i < n; i++) 
       if (mp[arr[i]] == 1)
            return arr[i];
    return -1;
}
  
// Driver code
int main()
{
    int arr[] = { 9, 4, 9, 6, 7, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << firstNonRepeating(arr, n);
    return 0;
}

Output:

6

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:

// Efficient CPP program to print all non-
// repeating elements.
#include <bits/stdc++.h>
using namespace std;
  
void firstNonRepeating(int arr[], int n)
{
    // Insert all array elements in hash
    // table
    unordered_map<int, int> mp;
    for (int i = 0; i < n; i++) 
       mp[arr[i]]++;
  
    // Traverse through map only and
    for (auto x : mp)
       if (x.second == 1)
            cout << x.first << " ";
}
  
// Driver code
int main()
{
    int arr[] = { 9, 4, 9, 6, 7, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    firstNonRepeating(arr, n);
    return 0;
}

Output:

7 6


My Personal Notes arrow_drop_up

geek developer programming enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t