Given an array arr[] of integers which may or may not contain duplicate elements. Your task is to remove duplicate elements. Your result should have elements according their first appearance in the input array.
Examples:
Input: arr[] = [1, 2, 3, 1, 4, 2]
Output: [1, 2, 3, 4]
Explanation: 2 and 1 have more than 1 occurence.Input: arr[] = [1, 2, 3, 4]
Output: [1, 2, 3, 4]
Explanation: There is no duplicate element.
Table of Content
[Naive Approach] Using Nested loops - O(n^2) Time and O(1) Space
Use two nested loops. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the element is present on left side of it. If present, then ignore the element, else store it in result.
#include <iostream>
#include <vector>
using namespace std;
vector<int> removeDuplicate(vector<int> &arr)
{
vector<int> res;
for (int i = 0; i < arr.size(); i++)
{
// Check if this element is included in result
int j;
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
break;
// Include this element if not included previously
if (i == j)
res.push_back(arr[i]);
}
return res;
}
// Driver Code
int main()
{
vector<int> arr = {1, 2, 3, 1, 4, 2};
vector<int> res = removeDuplicate(arr);
for (int ele : res)
cout << ele << " ";
return 0;
}
import java.util.ArrayList;
import java.util.List;
public class GfG {
public static List<Integer> removeDuplicate(List<Integer> arr) {
List<Integer> res = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
// Check if this element is included in result
int j;
for (j = 0; j < i; j++)
if (arr.get(i) == arr.get(j))
break;
// Include this element if not included previously
if (i == j)
res.add(arr.get(i));
}
return res;
}
// Driver Code
public static void main(String[] args) {
List<Integer> arr = List.of(1, 2, 3, 1, 4, 2);
List<Integer> res = removeDuplicate(arr);
for (int ele : res)
System.out.print(ele + " ");
}
}
def removeDuplicate(arr):
res = []
for i in range(len(arr)):
found = False
# Check if already present
for j in range(i):
if arr[i] == arr[j]:
found = True
break
# If not found, add to result
if not found:
res.append(arr[i])
return res
# Driver Code
arr = [1, 2, 3, 1, 4, 2]
res = removeDuplicate(arr)
for ele in res:
print(ele, end=' ')
using System;
using System.Collections.Generic;
public class GfG
{
public static List<int> removeDuplicate(List<int> arr)
{
List<int> res = new List<int>();
for (int i = 0; i < arr.Count; i++)
{
// Check if this element is included in result
int j;
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
break;
// Include this element if not included previously
if (i == j)
res.Add(arr[i]);
}
return res;
}
// Driver Code
public static void Main()
{
List<int> arr = new List<int> { 1, 2, 3, 1, 4, 2 };
List<int> res = removeDuplicate(arr);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
function removeDuplicate(arr) {
let res = [];
for (let i = 0; i < arr.length; i++) {
// Check if this element is included in result
let j;
for (j = 0; j < i; j++)
if (arr[i] === arr[j])
break;
// Include this element if not included previously
if (i === j)
res.push(arr[i]);
}
return res;
}
// Driver Code
let arr = [1, 2, 3, 1, 4, 2];
let res = removeDuplicate(arr);
for (let ele of res)
console.log(ele + ' ');
Output
1 2 3 4
[Expected Approach] Using Hash Set - O(n) Time and O(n) Space
Use Hash Set to store distinct element. Insert all the elements in a hash set and then traverse the hash set to store the distinct elements in the resultant array.
- Create an empty hash set to store unique elements.
- Create a result vector to store the final answer.
- Traverse the given array from left to right. For each element, check if it is present in the set.
- If not present, insert it into the set and add it to the result vector.
- Return the result vector containing only unique elements.
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
vector<int> removeDuplicate(vector<int> &arr)
{
// Creates an empty hashset
int n = arr.size();
unordered_set<int> s;
vector<int> v;
// Traverse the input array
for (int i = 0; i < n; i++)
{
// If not present, then put it in
// hashtable and print it
if (s.find(arr[i]) == s.end())
{
s.insert(arr[i]);
v.push_back(arr[i]);
}
}
return v;
}
// Driver Code
int main()
{
vector<int> arr = {1, 2, 3, 1, 4, 2};
vector<int> res = removeDuplicate(arr);
for (int ele : res)
cout << ele << " ";
return 0;
}
import java.util.HashSet;
import java.util.ArrayList;
public class GfG {
public static ArrayList<Integer> removeDuplicate(int[] arr)
{
// Creates an empty hashset
HashSet<Integer> s = new HashSet<>();
ArrayList<Integer> v = new ArrayList<>();
// Traverse the input array
for (int i = 0; i < arr.length; i++) {
// If not present, then put it in
// hashtable and print it
if (!s.contains(arr[i])) {
s.add(arr[i]);
v.add(arr[i]);
}
}
return v;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 2, 3, 1, 4, 2 };
ArrayList<Integer> res = removeDuplicate(arr);
for (int ele : res)
System.out.print(ele + " ");
}
}
def removeDuplicate(arr):
# Creates an empty hashset
s = set()
v = []
# Traverse the input array
for i in range(len(arr)):
# If not present, then put it in
# hashtable and print it
if arr[i] not in s:
s.add(arr[i])
v.append(arr[i])
return v
# Driver Code
arr = [1, 2, 3, 1, 4, 2]
res = removeDuplicate(arr)
for ele in res:
print(ele, end=' ')
using System;
using System.Collections.Generic;
public class GfG {
public static List<int> removeDuplicate(int[] arr)
{
// Creates an empty hashset
HashSet<int> s = new HashSet<int>();
List<int> v = new List<int>();
// Traverse the input array
for (int i = 0; i < arr.Length; i++)
{
// If not present, then put it in
// hashtable and print it
if (!s.Contains(arr[i]))
{
s.Add(arr[i]);
v.Add(arr[i]);
}
}
return v;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 2, 3, 1, 4, 2 };
List<int> res = removeDuplicate(arr);
foreach (int ele in res)
Console.Write(ele + " ");
}
}
function removeDuplicate(arr) {
// Creates an empty hashset
let s = new Set();
let v = [];
// Traverse the input array
for (let i = 0; i < arr.length; i++) {
// If not present, then put it in
// hashtable and print it
if (!s.has(arr[i])) {
s.add(arr[i]);
v.push(arr[i]);
}
}
return v;
}
// Driver Code
let arr = [1, 2, 3, 1, 4, 2];
let res = removeDuplicate(arr);
for (let ele of res) {
console.log(ele + " ");
}
Output
1 2 3 4