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] Nested Loops - O(n^2) 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++) {
bool found = false;
// Check if current element
// appears elsewhere in array
for (int j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
found = true;
break;
}
}
// If no duplicate found, return this element
if (!found)
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++) {
boolean found = false;
// Check if current element
// appears elsewhere in array
for (int j = 0; j < n; j++) {
if (i != j && arr[i] == arr[j]) {
found = true;
break;
}
}
// If no duplicate found, return this element
if (!found)
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):
found = False
# Check if it appears elsewhere
for j in range(n):
if i != j and arr[i] == arr[j]:
found = True
break
# If no duplicate found
if not found:
return arr[i]
return 0
if __name__ == "__main__":
arr = [-1, 2, -1, 3, 2]
print(firstNonRepeating(arr))
using System;
class GFG {
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++)
{
bool found = false;
// Check if current element appears elsewhere in array
for (int j = 0; j < n; j++)
{
if (i != j && arr[i] == arr[j])
{
found = true;
break;
}
}
// If no duplicate found, return this element
if (!found)
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++) {
let found = false;
// Check if it appears elsewhere
for (let j = 0; j < n; j++) {
if (i !== j && arr[i] === arr[j]) {
found = true;
break;
}
}
// If no duplicate found
if (!found)
return arr[i];
}
return 0;
}
// Driver code
let arr = [-1, 2, -1, 3, 2];
console.log(firstNonRepeating(arr));
Output
3
[Expected Approach] Hash Map - O(n) Time and O(n) Space
A hash map stores the frequency of each element in the array. First, traverse
arr[]and count the occurrences of each element. Then, traversearr[]again from left to right and return the first element whose frequency is1. If no such element exists, return0.
Consider 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)
{
Dictionary<int, int> map = new Dictionary<int, int>();
// Store the frequency of each element
foreach (int num in arr)
map[num] = map.ContainsKey(num) ? map[num] + 1 : 1;
// Find the first element with frequency 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