Given an array arr[] of integers of size n, find the first non-repeating element in this array. if no such element exists return 0.
Examples:
Input: arr[] = [-1, 2, -1, 3, 2]
Output: 3
Explanation: -1 and 2 are repeating whereas 3 is the only number occuring once. Hence, the output is 3.Input: arr[] = [1, 1, 1]
Output: 0
Explanation: There is not present any non-repeating element so answer should be 0.
Table of Content
[Naive Approach] Using Nested Loops - O(n * n) Time and O(1) Space
A non-repeating element appears only once, so iterate through the array from left to right, and for each element, scan the array to check if it appears again at any other index. If you find a duplicate, skip it, if you don’t find any, return that element as the first non-repeating one. If every element has a duplicate, return 0.
#include <iostream>
#include <vector>
using namespace std;
int firstNonRepeating(vector<int>& arr) {
int n = arr.size();
// Loop for checking each element (left to right)
for (int i = 0; i < n; i++) {
// Check if current element
// appears elsewhere in array
int j;
for (j = i+1; j < n; j++) {
if (arr[i] == arr[j])
break;
}
// If no duplicate found, return this element
if (j == n)
return arr[i];
}
// If all elements repeat, return 0
return 0;
}
int main() {
vector<int> arr = {-1, 2, -1, 3, 2};
cout << firstNonRepeating(arr);
return 0;
}
import java.util.Arrays;
public class Main {
public static int firstNonRepeating(int[] arr) {
int n = arr.length;
// Loop for checking each element (left to right)
for (int i = 0; i < n; i++) {
// Check if current element
// appears elsewhere in array
int j;
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j])
break;
}
// If no duplicate found, return this element
if (j == n)
return arr[i];
}
// If all elements repeat, return 0
return 0;
}
public static void main(String[] args) {
int[] arr = {-1, 2, -1, 3, 2};
System.out.println(firstNonRepeating(arr));
}
}
def firstNonRepeating(arr):
n = len(arr)
# Check each element
for i in range(n):
# Check if it appears again
j = i + 1
while j < n:
if arr[i] == arr[j]:
break
j += 1
# If no duplicate found
if j == n:
return arr[i]
return 0
arr = [-1, 2, -1, 3, 2]
print(firstNonRepeating(arr))
using System;
class Program
{
public static int FirstNonRepeating(int[] arr)
{
int n = arr.Length;
// Loop for checking each element (left to right)
for (int i = 0; i < n; i++)
{
// Check if current element appears elsewhere in array
int j;
for (j = i + 1; j < n; j++)
{
if (arr[i] == arr[j])
break;
}
// If no duplicate found, return this element
if (j == n)
return arr[i];
}
// If all elements repeat, return 0
return 0;
}
static void Main(string[] args)
{
int[] arr = { -1, 2, -1, 3, 2 };
Console.WriteLine(FirstNonRepeating(arr));
}
}
function firstNonRepeating(arr) {
let n = arr.length;
// Check each element
for (let i = 0; i < n; i++) {
// Check if it appears again
let j;
for (j = i + 1; j < n; j++) {
if (arr[i] === arr[j])
break;
}
// If no duplicate found
if (j === n)
return arr[i];
}
return 0;
}
let arr = [-1, 2, -1, 3, 2];
console.log(firstNonRepeating(arr));
Output
3
[Expected Approach] Using Hash Map - O(n) Time and O(n) Space
- 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.
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.
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int firstNonRepeating(const vector<int>& arr) {
unordered_map<int, int> mp;
// Store frequency of each element
for (int num : arr)
mp[num]++;
// Traverse from left to right to find first with freq 1
for (int num : arr)
if (mp[num] == 1)
return num;
// If all elements repeat
return 0;
}
int main() {
vector<int> arr = {-1, 2, -1, 3, 2};
cout << firstNonRepeating(arr);
return 0;
}
import java.util.HashMap;
public class GFG {
static int firstNonRepeating(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
// Store frequency
for (int num : arr)
map.put(num, map.getOrDefault(num, 0) + 1);
// Find first with freq 1
for (int num : arr)
if (map.get(num) == 1)
return num;
return 0;
}
public static void main(String[] args) {
int[] arr = {-1, 2, -1, 3, 2};
System.out.println(firstNonRepeating(arr));
}
}
def firstNonRepeating(arr):
freq = {}
# Store frequency
for num in arr:
freq[num] = freq.get(num, 0) + 1
# Find first with freq 1
for num in arr:
if freq[num] == 1:
return num
return 0
if __name__ == "__main__":
arr = [-1, 2, -1, 3, 2]
print(firstNonRepeating(arr))
using System;
using System.Collections.Generic;
class GFG
{
static int FirstNonRepeating(int[] arr)
{
var map = new Dictionary<int, int>();
// Store frequency
foreach (int num in arr)
map[num] = map.ContainsKey(num) ? map[num] + 1 : 1;
// Find first with freq 1
foreach (int num in arr)
if (map[num] == 1)
return num;
return 0;
}
static void Main()
{
int[] arr = { -1, 2, -1, 3, 2 };
Console.WriteLine(FirstNonRepeating(arr));
}
}
function firstNonRepeating(arr) {
const map = new Map();
// Store frequency
for (const num of arr)
map.set(num, (map.get(num) || 0) + 1);
// Find first with freq 1
for (const num of arr)
if (map.get(num) === 1)
return num;
return 0;
}
// Driver code
const arr = [-1, 2, -1, 3, 2];
console.log(firstNonRepeating(arr));
Output
3