Updated April 13, 2023
Introduction to C++ unique()
In C unique(), a unique function is a standard library function that specifically works for removing duplicates of any elements which are found or are present consecutively in a range [first, last). If all the elements present in the range are in the same format and in the same sequence consecutively then there are very rare chances of the presence of duplicate elements. It is not true that all the duplicate elements present get removed but it removes the duplicate elements to some level by replacing those elements which are the very next element present within the sequence that gets replaced are left in an unspecified state.
Syntax
The syntax flow is made in the following way :
Equality_1st
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator frst, ForwardIterator lst);
Predicate_2nd
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator frst, ForwardIterator lst,
BinaryPredicate prd);
Explanation: first is a forward iterator which points towards the position of the first element in the range from first to last to be scanned for duplicate removal of elements. Last is the final iterator pointing towards the position of the element which is one past the final element in the range used for scanning of duplicate removal. third is a kind of user defined-function object which defines with the satisfaction of the condition for a satisfactory condition where the two elements in a range to be taken as equivalent. Any binary predicate returns arguments more than once and returns true if satisfied and false when not satisfied. Forward iterator which points towards the new end of the range from first to the last returns the consecutive elements without any duplicate elements in place.
How does unique() function work in C++?
Let us understand how does this function work:
unique() function in C++ have lots of added advantages that help programmers to remove delicacy of elements within the specific range from first to last of the given range. There are some special added standard libraries that take care of the iterator traversal and then it helps in giving programmers the notion about how to move and travel with the elements within the range. There are some forward Iterators that involve both Equality and Predicate conditions. Equality condition checks and points towards the position of the first element in the specified range from first to last for scanning and checking duplicate elements.
The main aim of using this function is to remove and get all the elements with non-duplicate and pure elements. The return value contains no duplicate elements with a forward iterator specifying and pointing towards the elements lying within the range from first and last. Another value also includes a predicate with the iterator pointing towards the Forward iterator pointing towards the new end of the range from first to last which in turn returns the elements in sequential order and then it will help in getting the elements in the desired place without any presence of the duplicate elements in place. The complexity lies with a fact that the complexity is linear in nature and then it compares and checks for all the elements lying within the range from first and last.
The complexity factor involves a comparison of all the elements in a sequence and then performs some assignment or general operations. The objects present within the Data which performs a race condition and can be accessed or can eventually modify it for later changes. The unique function has one more special feature which says that the function throws an exception if any of the pred, element comparisons, element assignments, or any of the operations throws an exception in terms of an iterator for performing further operations.
Examples to Implement C++ unique()
Let us understand examples mentioned:
Example #1
This program illustrates the use of unique functionality by declaring a vector with specified elements and then removing all the elements from the first and last of the function.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main()
{
vector<int> vctr{1,5,5,4,3,2,8,6,6,6,2,2,2,1};
sort(vctr.begin(), vctr.end());
auto lst = unique(vctr.begin(), vctr.end());
vctr.erase(lst, vctr.end());
for (int m : vctr)
cout << m << " ";
cout << "\n";
return 0;
}
Output:
Example #2
This program illustrates the unique example with the fact where the default comparison is made with the predicate comparison and printing the value in the console as shown which do not include any of the duplicate elements within the array and the function for elements with the iterator and the traversals including some standard libraries as functionality.
Code:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool m_func(int a, int b) {
return (a==b);
}
int main () {
int m_int[] = {8,10,14,12,12,10,9,8,8,7};
vector<int> m_vect (m_int,m_int+9);
vector<int>::iterator tr;
tr = unique (m_vect.begin(), m_vect.end());
m_vect.resize( distance(m_vect.begin(),tr) );
unique (m_vect.begin(), m_vect.end(), m_func);
std::cout << "m_vect contains:";
for (tr=m_vect.begin(); tr!=m_vect.end(); ++tr)
cout << ' ' << *tr;
cout << '\n';
return 0;
}
Output:
Example #3
This program demonstrates the unique function to count for the number of elements in the vector declared for counting all the unique elements by removing all the duplicate elements.
Code:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> vctr = { 2,2,4,5,8,9,1,3,4,6};
vector<int>::iterator _ip;
int count;
sort(vctr.begin(), vctr.end());
count = std::distance(vctr.begin(),
std::unique(vctr.begin(), vctr.begin() + 12));
cout << "Total_unique_elements = " << count;
return 0;
}
Output:
Advantages of C++ unique()
Every function in C++ have some advantages so do this function which is as follows :
It is a seamless function that is used for removing the duplicate elements from the container thus the searching and memory utilization remains proper.
It has the pre-defined templates which are used for comparing elements and then removing all the elements one by one especially the duplicate elements to fetch the proper elements in a sequence.
Conclusion
It is an advantage for the programmers in terms of execution and implementation of the code snippets based on the searching and sorting elements which don’t contain any of the duplicate elements for manipulation although the container is not having duplicate elements it is capable of performing traversals to and from easily because of the C++ unique function.
Recommended Articles
This is a guide to C++ unique(). Here we discuss an introduction to C++ unique(), how does it work, advantages with programming examples. You can also go through our other related articles to learn more –