Finding duplicate elements which are repeating more than once.

The code gives repeated elements as a output. But when I tried to make digit being repeated four or five times, my output gets wrong. Here is the code

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cout << "Enter the size of array:- ";
    cin >> n;
    cout << "Enter the values:- \n";
    int arr[n - 1],i;
    for (i = 0; i < n; i++){
        cin >> arr[i];
    }
    sort(arr, arr+n);//convert an array into ascending
    cout<<"Repeated digits are following:- "<<endl;
    for (i = 0; i < n; i++){
        if(arr[i]==arr[i+1]){ //checks alternate elements
            cout << arr[i] << endl;
            arr[i+1] = {0};// makes zero the alernate element   
        }
        i++;// increase value of i
    }
        return 0;
}
Comments (0)