How to Find the Unique Elements in an Array in C++? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the unique elements in an array in C++. Example: Input: array = {1, 2, 1, 2, 2, 3, 4} Output: Unique elements in the array: 1 2 3 4Find the Unique Elements in an Array in C++To find the Unique elements in a std::array in C++, we can iterate through the array and store the array elements in a std::set because the set will store the unique elements of the array and ignore the duplicate values. C++ Program to Find Unique Elements in an ArrayThe below program demonstrates how we can find the unique elements in an array in C++. C++ // C++ Program to illustrate how to find unique elements in // an array #include <iostream> #include <set> #include <vector> using namespace std; // driver code int main() { vector<int> arr = { 1, 2, 1, 2, 2, 3, 4 }; set<int> s; // inserting elements in the set. s.insert(arr.begin(), arr.end()); cout << "Unique elements in the array: "; for (auto it = s.begin(); it != s.end(); ++it) cout << ' ' << *it; return 0; } OutputUnique elements in the array: 1 2 3 4Time Complexity: O(N * log(N)), where N is the number of elements in the set.Auxiliiary Space: O(N) Comment More infoAdvertise with us Next Article How to Find the Unique Elements in an Array in C++? A akshitsaxena2 Follow Improve Article Tags : C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads Remove duplicate elements in an Array using STL in C++ Given an array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: This can be done using set in standard template library. Set 2 min read Find the Frequency of Each Element in a Vector in C++ The frequency of an element is number of times it occurred in the vector. In this article, we will learn different methods to find the frequency of each element in a vector in C++.The simplest method to find the frequency of each element in a vector is by iterating the vector and storing the count o 3 min read Elements that occurred only once in the array Given an array arr that has numbers appearing twice or once. The task is to identify numbers that occur only once in the array. Note: Duplicates appear side by side every time. There might be a few numbers that can occur at one time and just assume this is a right rotating array (just say an array c 15+ min read Searching Elements in an Array | Array Operations In this post, we will look into search operation in an Array, i.e., how to search an element in an Array, such as: Searching in an Unsorted Array using Linear SearchSearching in a Sorted Array using Linear SearchSearching in a Sorted Array using Binary SearchSearching in an Sorted Array using Fibona 15+ min read Queries for number of distinct elements from a given index till last index in an array Given a array âa[]â of size n and number of queries q. Each query can be represented by a integer m. Your task is to print the number of distinct integers from index m to n i.e till last element of the array. Examples: Input: arr[] = {1, 2, 3, 1, 2, 3, 4, 5}, q[] = {1, 4, 6, 8} Output: 5 5 3 1 In qu 6 min read Remove duplicates from a sorted array using STL in C++ Given a sorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can be removed using the un 2 min read Remove duplicates from an unsorted array using STL in C++ Given an unsorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {1, 2, 5, 1, 7, 2, 4, 2} Output: arr[] = {1, 2, 4, 5, 7} Input: arr[] = {1, 2, 4, 3, 5, 4, 4, 2, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can 2 min read forward_list::unique() in C++ STL forward_list::unique() is an inbuilt function in C++ STL which removes all consecutive duplicate elements from the forward_list. It uses binary predicate for comparison. Syntax: forwardlist_name.unique(BinaryPredicate name)Parameters: The function accepts a single parameter which is a binary predica 2 min read How to Erase Duplicates and Sort a Vector in C++? In this article, we will learn how to remove duplicates and sort a vector in C++.The simplest method to remove the duplicates and sort the vector is by using sort() and unique() functions. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i 3 min read std::adjacent_find in C++ Searches the range [first, last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found. Elements are compared using the given binary predicate p or using ==. There are two possible implementations 3 min read Like