Check if all elements in array are unique in C++

This tutorial will discuss about unique ways to check if all elements in array are unique in C++.

Table Of Contents

Technique 1: Using std::unique() and std::distance()

To check if there are no duplicate elements in an array, first we will sort the array contents suing std::sort() function.

Then using the std::unique() function, we will shift all the duplicate elements to the end of array. std::unique() will return an iterator pointing to the element next to last unqiue element in array. If there are no duplicate elements in the array, then the distance between the iterator pointing to the start of array, and iterator returned by std::unique() will be equal to the length of array. But it will change the order of elements in array.

Let’s see the complete example,

#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = { 33, 44, 89, 67, 51, 30 };

    // Get the length of array
    size_t len = sizeof(arr)/sizeof(arr[0]);

    // Sort the values in array
    std::sort(arr, arr + len);

    // Check if all elements in array are unique
    bool result = std::distance(arr, std::unique(arr, arr + len)) == len;

    if (result)
    {
        std::cout << "Yes, all elements in the array are unique." << std::endl;
    }
    else
    {
        std::cout << "No, All elements in the array are not unique." << std::endl;
    }

    return 0;
}

Output :

Yes, all elements in the array are unique.

Technique 2: Using std::adjacent_find()

The std::adjacent_find() function accepts the start and end pointers of an array, and returns the pointer pointing to the element which is equal to its next element. If there is no such element in the array, then it returns an pointer, pointing to the end of array.

So, to check if array has only unique elements, first sort the array using the std::sort() function.

Then using the std::adjacent_find() function, check if any two adjacent values are equal in the sorted array. If yes, then it means all the elements in array are not unique.

Let’s see the complete example,

#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = { 33, 44, 89, 67, 51, 30 };

    // Get the lenth of array
    size_t len = sizeof(arr)/sizeof(arr[0]);

    // Sort the values in array
    std::sort(arr, arr + len);

    // Check if two adjacent elements in array are equal
    bool result = std::adjacent_find(arr, arr + len) == arr + len;

    // If any two adjacent elements are equal in sorted array,
    // then it means array has duplicate elements
    if (result)
    {
        std::cout << "Yes, all elements in the array are unique." << std::endl;
    }
    else
    {
        std::cout << "No, All elements in the array are not unique." << std::endl;
    }

    return 0;
}

Output :

Yes, all elements in the array are unique.

Summary

Today we learned about several ways to check if all elements in array are unique in C++. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top