You should initialize last
, otherwise it will be filled with garbage and according to your logic, it will print last
.
int last = arr[lengthOfArray-1];
int count = 0;
for(int i = lengthOfArray-1; i >=0; i--){
if(last == arr[i]){
++count;
} else{
cout << last << endl;
count = 1;
}
last = arr[i];
}
if (count > 0) {
cout << last << endl;
}
solved printing array in reverse order avoiding duplicates c++