# Remove duplicates from an array of small primes

Given an array of primes such that the range of primes is small. Remove duplicates from the array.

Examples:

```Input :  arr[] = {3, 5, 7, 2, 2, 5, 7, 7};
Output : arr[] = {2, 3, 5, 7}
The output can be printed in any order.

Input :  arr[] = {3, 5, 7, 3, 3, 13, 5, 13, 29, 13};
Output : arr[] = {3, 5, 7, 13, 29}
The output can be printed in any order.
```

Source : Amazon Interview Question

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1 (Naive : O(n2))
A simple solution is to run two loops. Pick all elements one by one. For every picked element, check if it already seen or not. If already seen, then ignore it. Else add it to the array.

## C++

 `// A C++ program to implement Naive approach to  ` `// remove duplicates. ` `#include ` `using` `namespace` `std; ` ` `  `int` `removeDups(vector<``int``> &vect) ` `{ ` `   ``int` `res_ind = 1; ` `   `  `   ``// Loop invariant : Elements from vect ` `   ``// to vect[res_ind-1] are unique. ` `   ``for` `(``int` `i=1; i vect{3, 5, 7, 2, 2, 5, 7, 7}; ` `    ``removeDups(vect); ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to implement Naive approach to  ` `// remove duplicates ` `class` `GFG  ` `{ ` `    ``static` `int``[] removeDups(``int``[] vect) ` `    ``{ ` `        ``int` `res_ind = ``1``; ` ` `  `        ``// Loop invariant : Elements from vect ` `        ``// to vect[res_ind-1] are unique. ` `        ``for` `(``int` `i = ``1``; i < vect.length; i++)  ` `        ``{ ` `            ``int` `j; ` `            ``for` `(j = ``0``; j < i; j++) ` `                ``if` `(vect[i] == vect[j]) ` `                    ``break``; ` `            ``if` `(j == i) ` `                ``vect[res_ind++] = vect[i]; ` `        ``} ` ` `  `        ``// Removes elements from vect[res_ind]  ` `        ``// to vect[end] ` `        ``int``[] new_arr = ``new` `int``[res_ind]; ` `        ``for` `(``int` `i = ``0``; i < res_ind; i++) ` `            ``new_arr[i] = vect[i]; ` ` `  `        ``return` `new_arr; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int``[] vect = { ``3``, ``5``, ``7``, ``2``, ``2``, ``5``, ``7``, ``7` `}; ` `        ``vect = removeDups(vect); ` ` `  `        ``for` `(``int` `i = ``0``; i < vect.length; i++) ` `            ``System.out.print(vect[i] + ``" "``); ` `        ``System.out.println(); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

## Python3

 `# A Python3 program to implement  ` `# Naive approach to remove duplicates. ` `def` `removeDups(vect): ` ` `  `    ``res_ind ``=` `1` ` `  `# Loop invariant : Elements from vect ` `# to vect[res_ind-1] are unique. ` `    ``for` `i ``in` `range``(``1``, ``len``(vect)): ` `        ``j ``=` `0` `        ``while` `(j < i): ` `            ``if` `(vect[i] ``=``=` `vect[j]): ` `                ``break` `            ``j ``+``=` `1` `        ``if` `(j ``=``=` `i): ` `            ``vect[res_ind] ``=` `vect[i] ` `            ``res_ind ``+``=` `1` ` `  `# Removes elements from ` `# vect[res_ind] to vect[end] ` `    ``return` `vect[``0``:res_ind] ` ` `  `# Driver code ` `vect ``=` `[``3``, ``5``, ``7``, ``2``, ``2``, ``5``, ``7``, ``7``] ` `vect ``=` `removeDups(vect) ` `for` `i ``in` `range``(``len``(vect)): ` `    ``print``(vect[i], end ``=` `" "``) ` `     `  `# This code is contributed  ` `# by mohit kumar `

## C#

 `// C# program to implement Naive approach  ` `// to remove duplicates ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``static` `int``[] removeDups(``int``[] vect) ` `    ``{ ` `        ``int` `res_ind = 1; ` ` `  `        ``// Loop invariant : Elements from vect ` `        ``// to vect[res_ind-1] are unique. ` `        ``for` `(``int` `i = 1; i < vect.Length; i++)  ` `        ``{ ` `            ``int` `j; ` `            ``for` `(j = 0; j < i; j++) ` `                ``if` `(vect[i] == vect[j]) ` `                    ``break``; ` `            ``if` `(j == i) ` `                ``vect[res_ind++] = vect[i]; ` `        ``} ` ` `  `        ``// Removes elements from vect[res_ind]  ` `        ``// to vect[end] ` `        ``int``[] new_arr = ``new` `int``[res_ind]; ` `        ``for` `(``int` `i = 0; i < res_ind; i++) ` `            ``new_arr[i] = vect[i]; ` ` `  `        ``return` `new_arr; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int``[] vect = { 3, 5, 7, 2, 2, 5, 7, 7 }; ` `        ``vect = removeDups(vect); ` ` `  `        ``for` `(``int` `i = 0; i < vect.Length; i++) ` `            ``Console.Write(vect[i] + ``" "``); ` `        ``Console.WriteLine(); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output :

`3 5 7 2 `

Time Complexity : O(n2)

Method 2 (Sorting : O(n Log n))
A better solutions is to first sort the array and then remove adjacent elements from sorted array.

 `// C++ program to remove duplicates using Sorting ` `#include ` `using` `namespace` `std; ` ` `  `int` `removeDups(vector<``int``> &vect) ` `{ ` `    ``// Sort the vector ` `    ``sort(vect.begin(), vect.end()); ` ` `  `    ``// unique() removes adjacent duplicates. ` `    ``// unique function puts all unique elements at  ` `    ``// the beginning and returns iterator pointing  ` `    ``// to the first element after unique element.  ` `    ``// Erase function removes elements between two ` `    ``// given iterators ` `    ``vect.erase(unique(vect.begin(), vect.end()), ` `              ``vect.end()); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> vect{3, 5, 7, 2, 2, 5, 7, 7}; ` `    ``removeDups(vect); ` `    ``for` `(``int` `i=0; i

Output :

`2 3 5 7 `

Time Complexity : O(n Log n)
Auxiliary Space : O(1)

Method 3 (Hashing : O(n))
The idea is keep track of visited elements in a hash table.

## C++

 `// C++ program to remove duplicates using Hashing ` `#include ` `using` `namespace` `std; ` ` `  `int` `removeDups(vector<``int``> &vect) ` `{ ` `    ``// Create a set from vector elements ` `    ``unordered_set<``int``> s(vect.begin(), vect.end()); ` ` `  `    ``// Take elements from set and put back in ` `    ``// vect[] ` `    ``vect.assign(s.begin(), s.end()); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``vector<``int``> vect{3, 5, 7, 2, 2, 5, 7, 7}; ` `    ``removeDups(vect); ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to implement Naive approach to  ` `// remove duplicates. ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``static` `void` `removeDups(Vector vect)  ` `    ``{ ` `     `  `        ``// Create a set from vector elements ` `        ``Set set = ``new` `HashSet(vect); ` `     `  `        ``// Take elements from set and put back in ` `        ``// vect[] ` `        ``vect.clear(); ` `        ``vect.addAll(set); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``Integer arr[] = {``3``, ``5``, ``7``, ``2``, ``2``, ``5``, ``7``, ``7``}; ` `        ``Vector vect = ``new` `Vector(Arrays.asList(arr)); ` `        ``removeDups(vect); ` `        ``for` `(``int` `i = ``0``; i < vect.size(); i++)  ` `        ``{ ` `            ``System.out.print(vect.get(i) + ``" "``); ` `        ``} ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## C#

 `// C# program to implement Naive approach to  ` `// remove duplicates. ` `using` `System; ` `using` `System.Collections.Generic; ` `using` `System.Linq; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``static` `List<``int``> removeDups(List<``int``> vect)  ` `    ``{ ` ` `  `        ``// Create a set from vector elements ` `        ``HashSet<``int``> ``set` `= ``new` `HashSet<``int``>(vect); ` ` `  `        ``// Take elements from set and put back in ` `        ``// vect[] ` `        ``vect.Clear(); ` `        ``vect = ``set``.ToList(); ` `        ``return` `vect; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `        ``int` `[]arr = {3, 5, 7, 2, 2, 5, 7, 7}; ` `        ``List<``int``> vect = ``new` `List<``int``>(arr); ` `        ``vect = removeDups(vect); ` `        ``for` `(``int` `i = 0; i < vect.Count; i++)  ` `        ``{ ` `            ``Console.Write(vect[i] + ``" "``); ` `        ``} ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output :

`2 7 5 3`

Time Complexity : O(n)
Auxiliary Space : O(n)

Method 4 (Works only for small range : O(n))
This solutions uses the fact that numbers are primes. But it works only when product of all distinct primes in array is less than maximum value in long long int.

## C++

 `// Removes duplicates using multiplication of  ` `// distinct primes in array ` `#include ` `using` `namespace` `std; ` ` `  `int` `removeDups(vector<``int``> &vect) ` `{ ` `   ``long` `long` `int` `prod = vect; ` `   ``int` `res_ind = 1; ` `   ``for` `(``int` `i=1; i vect{3, 5, 7, 2, 2, 5, 7, 7}; ` `    ``removeDups(vect); ` `    ``for` `(``int` `i=0; i

## Java

 `// Removes duplicates using multiplication of  ` `// distinct primes in array ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `[]removeDups(``int` `[]vect) ` `{ ` `     `  `    ``int` `prod = vect[``0``]; ` `    ``int` `res_ind = ``1``; ` `    ``for` `(``int` `i = ``1``; i < vect.length; i++) ` `    ``{ ` `        ``if` `(prod % vect[i] != ``0``) ` `        ``{ ` `            ``vect[res_ind++] = vect[i]; ` `            ``prod *= vect[i]; ` `        ``} ` `    ``} ` `    ``return` `Arrays.copyOf(vect, res_ind); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `        ``int` `[]vect = {``3``, ``5``, ``7``, ``2``, ``2``, ``5``, ``7``, ``7``}; ` `        ``vect = removeDups(vect); ` `        ``for` `(``int` `i = ``0``; i < vect.length; i++) ` `        ``System.out.print(vect[i]+ ``" "``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## C#

 `// Removes duplicates using multiplication of  ` `// distinct primes in array ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `[]removeDups(``int` `[]vect) ` `{ ` `     `  `    ``int` `prod = vect; ` `    ``int` `res_ind = 1; ` `    ``for` `(``int` `i = 1; i < vect.Length; i++) ` `    ``{ ` `        ``if` `(prod % vect[i] != 0) ` `        ``{ ` `            ``vect[res_ind++] = vect[i]; ` `            ``prod *= vect[i]; ` `        ``} ` `    ``} ` `    ``int``[]temp = ``new` `int``[vect.Length - res_ind]; ` `    ``Array.Copy(vect, 0, temp, 0, temp.Length); ` `    ``return` `temp; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]vect = {3, 5, 7, 2, 2, 5, 7, 7}; ` `    ``vect = removeDups(vect); ` `    ``for` `(``int` `i = 0; i < vect.Length; i++) ` `        ``Console.Write(vect[i]+ ``" "``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output :

`3 5 7 2`

Time Complexity : O(n)
Auxiliary Space : O(1)
Note that this solution would not work if there are composites in array.

This article is contributed by Shivam Mittal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.