last index of array c++
#include <iostream>
int findLastIndex(const int arr[], int size, int target) {
for (int i = size - 1; i >= 0; --i) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
int main() {
const int size = 5;
int array[size] = {1, 2, 3, 4, 3};
int target = 3;
int lastIndex = findLastIndex(array, size, target);
if (lastIndex != -1) {
std::cout << "Last index of " << target << " is: " << lastIndex << std::endl;
} else {
std::cout << target << " not found in the array." << std::endl;
}
return 0;
}