Index of the Last Occurrence of an Element in an Array
To find the index of the last occurrence of an element in a C array, we iterate through the array and store the index whenever we find a match. By continuing this process until the end of the array, we obtain the index of the last occurrence.
Examples
1. Finding the Last Occurrence Using a for
Loop
In this example, we traverse the array using a for
loop, checking each element for a match. If a match is found, we update the index variable, which will eventually hold the last occurrence index.
main.c
#include <stdio.h>
int findLastOccurrence(int arr[], int size, int target) {
int lastIndex = -1; // Initialize to -1 (not found)
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
lastIndex = i; // Update lastIndex when a match is found
}
}
return lastIndex;
}
int main() {
int numbers[] = {1, 2, 3, 4, 2, 5, 2, 6};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 2;
int lastIndex = findLastOccurrence(numbers, size, target);
printf("Last occurrence of %d is at index: %d\n", target, lastIndex);
return 0;
}
Explanation:
- We define a function
findLastOccurrence()
that takes an array, its size, and the target element as input. - We initialize
lastIndex
to-1
, indicating that the element hasn’t been found yet. - We iterate through the array using a
for
loop. - Whenever we find an element that matches the target, we update
lastIndex
with the current index. - After the loop,
lastIndex
contains the index of the last occurrence or remains-1
if the element is not found. - In
main()
, we call this function and print the result.
Output:
Last occurrence of 2 is at index: 6
2. Finding the Last Occurrence Using a Reverse Loop
Instead of scanning from the beginning, this example starts from the last index and moves backward, stopping as soon as the first match is found. This improves efficiency by reducing unnecessary iterations.
main.c
#include <stdio.h>
int findLastOccurrenceReverse(int arr[], int size, int target) {
for (int i = size - 1; i >= 0; i--) {
if (arr[i] == target) {
return i; // Return index immediately when found
}
}
return -1; // Return -1 if not found
}
int main() {
int numbers[] = {3, 5, 7, 5, 9, 5, 10};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 5;
int lastIndex = findLastOccurrenceReverse(numbers, size, target);
printf("Last occurrence of %d is at index: %d\n", target, lastIndex);
return 0;
}
Explanation:
- The function
findLastOccurrenceReverse()
starts iterating from the last index of the array. - As soon as it finds a match with the target element, it immediately returns the index.
- If no match is found, it returns
-1
. - Since the function stops at the first match, it improves efficiency.
Output:
Last occurrence of 5 is at index: 5
3. Finding the Last Occurrence Using Recursion
In this example, we use recursion to find the last occurrence of an element in an array by checking from the last index first.
main.c
#include <stdio.h>
int findLastOccurrenceRecursive(int arr[], int size, int target) {
if (size == 0) return -1; // Base case: empty array
if (arr[size - 1] == target) return size - 1; // Check last element
return findLastOccurrenceRecursive(arr, size - 1, target); // Recur for remaining array
}
int main() {
int numbers[] = {4, 6, 8, 6, 10, 6, 12};
int size = sizeof(numbers) / sizeof(numbers[0]);
int target = 6;
int lastIndex = findLastOccurrenceRecursive(numbers, size, target);
printf("Last occurrence of %d is at index: %d\n", target, lastIndex);
return 0;
}
Explanation:
- The recursive function
findLastOccurrenceRecursive()
checks the last element first. - If it matches the target, it returns the index.
- Otherwise, it calls itself with
size - 1
to check the rest of the array. - The recursion stops when the array size becomes 0, returning
-1
if no match is found.
Output:
Last occurrence of 6 is at index: 5
Conclusion
In this tutorial, we covered different approaches to finding the last occurrence of an element in a C array: