
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print All Distinct Elements of a given integer array in C++
In this problem, we are given an array of integer values. Our task is to print all distinct elements of the array. The output should contain only distinct values.
Let’s take an example to understand the problem
Input: array = {1, 5, 7, 12, 1, 6, 10, 7, 5} Output: 1 5 7 12 6 10
To solve this problem, we will have to check elements of the array for uniqueness. For this, we will use two nested loops, the outer one will take values and the inner one will check the rest of the values with it. If more than one values exit print only one.
Example
This code shows the implementation of our solution,
#include <iostream> using namespace std; void printDistinctValues(int arr[], int n) { for (int i=0; i<n; i++){ int j; for (j=0; j<i; j++) if (arr[i] == arr[j]) break; if (i == j) cout<<arr[i]<<"\t"; } } int main(){ int arr[] = {1, 5, 7, 12, 1, 6, 10, 7, 5}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"Distinct values of the array are :\n"; printDistinctValues(arr, n); return 0; }
Output
Distinct elements of the array are − 1 5 6 7 10 12
This solution is easy but uses two loops which make its complexity of the order of n2.
A more complex method would be using sorting. In the sorted array, the occurrence of similar numbers becomes consecutive. Now, we can print distinct elements easily and it takes less space.
Example
Implementation of our logic −
#include <bits/stdc++.h> using namespace std; void printDistinctElements(int arr[], int n){ sort(arr, arr + n); for (int i=0; i<n; i++){ while (i < n-1 && arr[i] == arr[i+1]) i++; cout<<arr[i]<<"\t"; } } int main(){ int arr[] = {1, 5, 7, 12, 1, 6, 10, 7, 5}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"Distinct elements of the array are :\n"; printDistinctElements(arr, n); return 0; }
Output
Distinct elements of the array are − 1 5 6 7 10 12
Another more effective solutions are by keeping track of visited elements of the array. We will traverse the array and keep track of all visited elements of the array.
Example
This code shows the implementation of our solution,
#include<bits/stdc++.h> using namespace std; void printDistinctElements(int arr[],int n) { unordered_set<int> visited; for (int i=0; i<n; i++){ if (visited.find(arr[i])==visited.end()){ visited.insert(arr[i]); cout<<arr[i]<<"\t"; } } } int main () { int arr[] = {1, 5, 7, 12, 1, 6, 10, 7, 5}; int n=7; cout<<"Distinct numbers of the array are :\n"; printDistinctElements(arr,n); return 0; }
Output
Distinct numbers of the array are − 1 5 7 12 6 10
- Related Articles
- C# program to print all distinct elements of a given integer array in C#
- Python program to print all distinct elements of a given integer array.
- Java program to print all distinct elements of a given integer array in Java
- Print sorted distinct elements of array in C language
- Print all distinct permutations of a given string with duplicates in C++
- Print all possible combinations of r elements in a given array of size n in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Print all distinct characters of a string in order in C++
- Print all pairs of anagrams in a given array of strings in C++
- C# program to find all duplicate elements in an integer array
- Check if all array elements are distinct in Python
- Find all distinct subsets of a given set in C++
- Count distinct elements in an array in C++
- Print a number strictly less than a given number such that all its digits are distinct in C++
- Find distinct elements common to all rows of a Matrix in C++
