How to find unique numbers in an array using C++

In this tutorial, we are going to learn about how to find unique numbers in an array using C++. We can use various methods to solve this problem but in this tutorial, we are going to implement two best methods. First with the help of sorting and second with the help of hashing.

using sorting to find unique numbers in an array

We will sort the given array such that occurrences of same numbers in an array become consecutive. After which we can select the last occurrence of each repeating number such that we can print only unique numbers. As sorting will take O(nlogn) time and printing unique numbers in an array will take O(n) time. So, the time complexity of this method will be O(nlogn) time.

Code: find unique numbers in an array in C++

// C++ program to print all unique numbers in a given array using sorting
#include<bits/stdc++.h> 
using namespace std; 
void unique_number(int arr[], int n) 
{ 
  // Sorting the given array
  sort(arr, arr + n); 
    
  // Finding unique numbers
  for (int i=0; i<n; i++) 
    { 
    if(arr[i]==arr[i+1])
    {
    	continue;
    }
    else
    {
        cout<<arr[i]<<" ";
    }
    }
} 
  
// Driver program
int main() 
{ 
    int n;
    cout<<"Enter the length of array: ";
    cin>>n;
    int arr[n];
    cout<<"\nEnter numbers you want to take in array: ";
    for(int i=0;i<n;i++)
    {
    	cin>>arr[i];
    }
    cout<<"\nUnique numbers in given array are: ";
    unique_number(arr, n); 
    return 0; 
}

Input:

Enter the length of array: 6

Enter numbers you want to take in array: 10 15 10 8 7 8

Output:

Unique numbers in given array are: 7 8 10 15

using hashing to find unique numbers

In this method, we will simply traverse the array and keep track of the numbers which we have visited in a hash table, and we will print our required unique number. The time complexity of this method will be O(n) time.

C++ program:

// C++ program to print all unique numbers in a given array using hashing
#include<bits/stdc++.h>
using namespace std; 
void unique_number(int arr[], int n) 
{ 
    // Creating a blank hashset
    unordered_set<int> unique; 
   
    for (int i=0; i<n; i++) 
    { 
        // Printing unique number 
        if (unique.find(arr[i])==unique.end()) 
        { 
            unique.insert(arr[i]); 
            cout <<arr[i]<<" "; 
        } 
    } 
} 
  
// Driver program
int main() 
{ 
    int n;
    cout<<"Enter length of array: ";
    cin>>n;
    int arr[n];
    cout<<"\nEnter numbers you want to take in array: ";
    for(int i=0;i<n;i++)
    {
    	cin>>arr[i];
    }
    cout<<"\nUnique numbers in given array are: ";
    unique_number(arr, n); 
    return 0; 
}

Input:

Enter the length of array: 6
 
Enter numbers you want to take in array: 10 15 10 8 7 8

Output:

Unique numbers in given array are: 10 15 8 7

You may also learn,

Solution of N-Queen problem in C++ using Backtracking

How to Terminate a Loop in C++

Do not forget to comment if you find anything wrong in the post or you want to share some information regarding the same.

One response to “How to find unique numbers in an array using C++”

  1. --- says:

    Thx bro. this is the easiest one and the solution which i had in my head but could not define it. Thank youuu

Leave a Reply

Your email address will not be published. Required fields are marked *