C++ Program to find duplicate elements in array


In this program, You will learn how to find duplicate elements in array in C++.


List is: 1, 2, 12, 23, 12, 23

Duplicate elements is: 12 23


Example: How to find duplicate elements in array in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int i, j, n, v = 1;
    int arr[100];

    cout << "Enter Size of an Array :";
    cin>>n;
    cout << "\nEnter Array Elements :";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    cout << "\nDuplicate Elements :";
    for (i = 0; i < n; i++) {
        for (j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
                if (v == 1 && arr[j] != '\0') {
                    cout << arr[i] << " ";
                }
                arr[j] = '\0';
                v++;
            }
        }
        v = 1;
    }

    return 0;
}

Output:

Enter Size of an Array :5
Enter Array Elements :1 5 5 4 5
Duplicate Elements :5