samedi 12 septembre 2020

Find the same element in array in C++ [closed]

this is my code... When i enter more than 3 the same element of array the output also loop more than 3

for example:

Enter number of times: 5

Enter a Number:

1 , 2 ,3 , 1, 1

Same element: 1 , 1, 1 (it should be "1" only not "1 ,1, 1")

but if i enter

1 , 1 ,2 ,3 ,4

Same element: 1 (i think the bug is in the loop.. can someone help me .. im just a beginner... i hope you understand)

#include using namespace std;

int main(){

int times, number;

int max_count = 0;

cout<<"Ente Number of Times: ";
cin>>times;
int arr[times]; 

times = sizeof(arr)/sizeof(arr[0]);
cout<<"Enter a number: ";
cout<<"\n";

for(int i = 0 ; i < times ; i++){
    cin>>arr[i];
}
    for(int i = 0 ; i < times ; i++){

    for (int j = i+1 ; j < times ; j++)
    

        
            if(arr[i] == arr[j]){
            cout<<"Same element: "<<arr[i];
    }
}

return 0;

}

Aucun commentaire:

Enregistrer un commentaire