Skip to content
Related Articles

Related Articles

Remove duplicates from an array of small primes
  • Difficulty Level : Easy
  • Last Updated : 02 Feb, 2021

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}
All the duplicates are removed from 
the array. 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}
All the duplicates are removed from  
the array. The output can be printed in any order.

Source: Amazon Interview Question

Method 1: This method discusses the naive approach which takes O(n2) time complexity.

Approach: So the basic idea is to check for every element, whether it has occurred previously or not. Therefore the approach involves keeping two loops one to select the present element or index and the inner loop to check if the element has previously occurred or not.



Algorithm:

  1. Start by running two loops.
  2. Pick all elements one by one.
  3. For every picked element, check if it has already occurred or not.
  4. If already seen, then ignore it else add it to the array.

C++




// A C++ program to implement Naive
// approach to remove duplicates.
#include <bits/stdc++.h>
using namespace std;
 
int removeDups(vector<int>& vect)
{
    int res_ind = 1;
 
    // Loop invariant: Elements from vect[0]
    // to vect[res_ind-1] are unique.
    for (int i = 1; i < vect.size(); 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]
    vect.erase(vect.begin() + res_ind, vect.end());
}
 
// Driver code
int main()
{
    vector<int> vect{ 3, 5, 7, 2, 2, 5, 7, 7 };
    removeDups(vect);
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << " ";
    return 0;
}


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[0]
        // 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[0]
# 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[0]
        // 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

Complexity Analysis:

  • Time Complexity: O(n2). 
    As two nested loops is used so the time complexity becomes O(n2).
  • Space Complexity: O(n). 
    As an extra array is used to store the elements so the space complexity is O(n).

Method 2: This method involves the technique of Sorting which takes O(n log n) time.

Approach: In comparison to the previous approach, a better solution is to first sort the array and then remove all the adjacent elements which are similar, from sorted array.

Algorithm:

  1. First sort the array.
  2. The need for extra space can be cleverly avoided, keep two variables, first = 1 and i = 1 .
  3. Traverse the array from second element to end.
  4. For every element, if that element is not equal to the previous element then array[first++] = array[i], where i is the counter of loop.
  5. So the length of the array with no duplicates is first, remove the rest elements.

Note: In CPP there are few inbuilt functions like sort() to sort and unique() to remove adjacent duplicates. The unique() function puts all unique elements at the beginning and returns iterator pointing to the first element after unique element. The erase() function removes elements between two given iterators.

C++




// C++ program to remove duplicates using Sorting
#include <bits/stdc++.h>
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 < vect.size(); i++)
        cout << vect[i] << " ";
    return 0;
}


Java




// Java program to remove duplicates using Sorting
import java.util.*;
 
class GFG {
 
    static int[] removeDups(int[] vect)
    {
        // sort the array
        Arrays.sort(vect);
 
        // pointer
        int first = 1;
 
        // remove duplicate elements
        for (int i = 1; i < vect.length; i++)
            if (vect[i] != vect[i - 1])
                vect[first++] = vect[i];
 
        // mark rest of elements to INT_MAX
        for (int i = first; i < vect.length; i++)
            vect[i] = Integer.MAX_VALUE;
 
        return vect;
    }
 
    // 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++) {
            if (vect[i] == Integer.MAX_VALUE)
                break;
            System.out.print(vect[i] + " ");
        }
    }
}


Python3




# Python3 program to remove duplicates
# using Sorting
import sys
 
def removeDups(vect):
     
    # Sort the array
    vect.sort()
     
    # Pointer
    first = 1
 
    # Remove duplicate elements
    for i in range(1, len(vect)):
        if (vect[i] != vect[i - 1]):
            vect[first] = vect[i]
            first += 1
             
    # Mark rest of elements to INT_MAX
    for  i in range(first, len(vect)):
        vect[i] = sys.maxsize
 
    return vect
 
# Driver code
vect = [ 3, 5, 7, 2, 2, 5, 7, 7 ]
vect = removeDups(vect)
 
for i in range(len(vect)):
    if (vect[i] == sys.maxsize):
        break
         
    print(vect[i], end = " ")
 
# This code is contributed by avanitrachhadiya2155


C#




// C# program to remove duplicates using Sorting
using System;
class GFG
{
    static int[] removeDups(int[] vect)
    {
       
        // sort the array
        Array.Sort(vect);
 
        // pointer
        int first = 1;
 
        // remove duplicate elements
        for (int i = 1; i < vect.Length; i++)
            if (vect[i] != vect[i - 1])
                vect[first++] = vect[i];
 
        // mark rest of elements to INT_MAX
        for (int i = first; i < vect.Length; i++)
            vect[i] = int.MaxValue;
        return vect;
    }
 
    // Driver code
    public static void Main(params string[] args)
    {
        int[] vect = { 3, 5, 7, 2, 2, 5, 7, 7 };
        vect = removeDups(vect);
        for (int i = 0; i < vect.Length; i++)
        {
            if (vect[i] == int.MaxValue)
                break;
            Console.Write(vect[i] + " ");
        }
    }
}
 
// This code is contributed by rutvik_56.


Output : 



2 3 5 7

Complexity Analysis:

  • Time Complexity: O(n Log n). 
    For sorting the array O(n log n ) time complexity is required, and to remove adjacent elements O(n) time complexity is required.
  • Auxiliary Space : O(1)
    Since no extra space is required, the space complexity is constant.

Method 3: The method involves the technique of Hashing which takes O(n) time.

Approach: The time complexity in this method can be reduced but space complexity will take a toll. This involves the use of Hashing where the numbers are marked in a HashMap, so that if the number is again encountered then erase it from the array.

Algorithm:

  1. Use a hash set. HashSet stores only unique elements.
  2. It is known that if two same elements are put into a HashSet the HashSet stores only one element (all the duplicate element vanishes)
  3. Traverse the array from start to end.
  4. For every element, insert the element in HashSet
  5. Now Traverse the HashSet and put the elements in the HashSet in the array

C++




// C++ program to remove duplicates using Hashing
#include <bits/stdc++.h>
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 < vect.size(); i++)
        cout << vect[i] << " ";
    return 0;
}


Java




// Java program to implement Naive approach to
// remove duplicates.
import java.util.*;
 
class GFG {
 
    static void removeDups(Vector<Integer> vect)
    {
 
        // Create a set from vector elements
        Set<Integer> set = new HashSet<Integer>(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<Integer> vect
            = new Vector<Integer>(
                Arrays.asList(arr));
        removeDups(vect);
        for (int i = 0; i < vect.size(); i++) {
            System.out.print(vect.get(i) + " ");
        }
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3




# Python3 program to remove duplicates using Hashing
def removeDups():
    global vect
 
    # Create a set from vector elements
    s = set(vect)
     
    # Take elements from set and put back in
    # vect[]
    vect = s
     
# Driver code
vect = [3, 5, 7, 2, 2, 5, 7, 7]
removeDups()
for i in vect:
    print(i, end = " ")
 
# This code is contributed by shubhamsingh10


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

Complexity Analysis: 

  • Time Complexity: O(n).
    Since a single traversal is needed to enter all the elements in the hashmap, the time complexity is O(n).
  • Auxiliary Space: O(n).
    For storing elements in hashset or hashmap O(n) space complexity is needed.

Method 4: This focuses on small ranged values where the time complexity is O(n).

Approach: This approach only works when the product of all distinct primes is fewer than 10^18 and all the numbers in the array should be prime. The property of primes of having no divisors except 1 or that number itself is used to arrive at the solution. As the array elements are removed from the array, keep a value(product) which will contain the product of all distinct primes found previously in the array, so that if the element divides the product then it can be surely proved that the element has previously occurred in the array and hence the number will be rejected.

Algorithm:

  1. Initially keep a variable (p = 1).
  2. Traverse the array from start to end.
  3. While traversing, check whether p is divisible by the i-th element. If true, then erase that element.
  4. Else keep that element and update the product by multiplying that element with the product (p = p * arr[i])

C++




// Removes duplicates using multiplication of
// distinct primes in array
#include <bits/stdc++.h>
using namespace std;
 
int removeDups(vector<int>& vect)
{
    long long int prod = vect[0];
    int res_ind = 1;
    for (int i = 1; i < vect.size(); i++) {
        if (prod % vect[i] != 0) {
            vect[res_ind++] = vect[i];
            prod *= vect[i];
        }
    }
    vect.erase(vect.begin() + res_ind, vect.end());
}
 
// Driver code
int main()
{
    vector<int> vect{ 3, 5, 7, 2, 2, 5, 7, 7 };
    removeDups(vect);
    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << " ";
    return 0;
}


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


Python3




# Removes duplicates using multiplication of
# distinct primes in array
def removeDups(vect):
    prod = vect[0]
    res_ind = 1
    i = 1
    while (i < len(vect)):
        if (prod % vect[i] != 0):
            vect[res_ind] = vect[i]
            res_ind += 1
            prod *= vect[i]
        vect = vect[:res_ind + 2]
        i += 1
    return vect
     
# 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 SHUBHAMSINGH10


C#




// Removes duplicates using multiplication of
// distinct primes in array
using System;
 
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];
            }
        }
        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

Complexity Analysis:

  • Time Complexity: O(n).
    To traverse the array only once, time required is O(n).
  • Auxiliary Space: O(1).
    One variable p is needed, so the space complexity is constant.

Note: This solution would not work if there are any composites in the 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :