Check Array for Duplicates

Beginner

Introduction

In this lab, we will learn how to determine if a given array contains any duplicates or not, using the C++ programming language. This will be achieved by sorting the array, and then comparing adjacent elements for equality.

Write the Library and Main Function

Write the library and main function as follows.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

bool containsDuplicate(int n[], int m)
{
    // Create a flag to indicate the presence of duplicate
    int f = 0;

    // Sort the input array to check for duplicates
    sort(n, n + m);

    for (int i = 0; i < m - 1; i++)
    {
        if (n[i] == n[i + 1])
        {
            // set the flag to 1 and exit the loop if dupliate is found
            f = 1;
            break;
        }
    }

    if (f == 1) {
        // Duplicate found
        return true;
    }
    else {
        // No duplicates found
        return false;
    }
}

int main()
{
    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " ===== Program to check if any duplicates are present in the input array  ===== \n\n";

    int i, n1, n2;

    int a1[] = {2, 3, 1, 4, 5, 2, 8, 9};
    int a2[] = {2, 3, 1, 4, 5, 10, 8, 9};

    bool duplicate1 = false;
    bool duplicate2 = false;

    // Get the size of the two input arrays
    n1 = sizeof(a1) / sizeof(a1[0]);
    n2 = sizeof(a2) / sizeof(a2[0]);

    // Print the first input array
    cout << "\n\nThe elements of the first input array are :\n\n";

    for (i = 0; i < n1; i++)
    {
        cout << a1[i] << "  ";
    }

    // Check if the first input array contains duplicates
    duplicate1 = containsDuplicate(a1, n1);

    if (duplicate1) {
        cout << "\n\nThe first input array contains duplicate";
    }
    else {
        cout << "\n\nThe first input array does not contain any duplicate";
    }

    // Print the second input array
    cout << "\n\n\n\nThe elements of the second input array are :\n\n";

    for (i = 0; i < n2; i++)
    {
        cout << a2[i] << "  ";
    }

    // Check if the second input array contains duplicates
    duplicate2 = containsDuplicate(a2, n2);

    if (duplicate2) {
        cout << "\n\nThe second input array contains duplicate";
    }
    else {
        cout << "\n\nThe second input array does not contain any duplicate";
    }

    cout << "\n\n\n";

    return 0;
}

Run the Code

Save the code in ~/project/main.cpp file, and compile the code using:

g++ main.cpp -o main && ./main

Understand the Code

To check for duplicates in an array, we first created a function named containsDuplicate(). This function takes two arguments - the input array n[] and its size m.
The function flags found duplicates with value 1, and otherwise with value 0. To find duplicates, the input n[] is first sorted using the system defined sort() function. Then, adjacent elements in the sorted array are compared, and the flag is set if duplicates are found.
In the main function, two test arrays a1 and a2 are created, each with a potential duplicate. Using containsDuplicate(), we check each array for duplicates, and output the result.

if (duplicate1) {
    cout << "\n\nThe first input array contains duplicate";
}
else {
    cout << "\n\nThe first input array does not contain any duplicate";
}

This output will either indicate if the input array contains duplicates or not.

Summary

Duplications in arrays can be easily determined using C++ programming. By sorting the input array and then comparing adjacent elements, it is possible to develop an efficient flagging system to indicate duplicate values.

Other Tutorials you may like