Find the Last Element of an Array
Problem Statement
Given an array of elements, the task is to find and display the last element of the array.
The last element of an array is the element stored at index
(length of array − 1).
Why This Problem Is Important
This problem strengthens understanding of:
- Array length and indexing relationship
- Zero-based indexing
- Safe element access
- Boundary conditions in arrays
Accessing the last element is frequently required in:
- Stack implementations
- Sliding window problems
- Two-pointer techniques
- Range-based array problems
Input and Output Format
Input
Array: [5, 15, 25, 35, 45]
Output
Last Element of the Array: 45
Key Concept
- Arrays are zero-indexed
- Last element index = array_length - 1
- Accessing beyond this index causes runtime errors
- Always check if the array is non-empty
Important Condition
- If the array is empty, there is no last element
Step-by-Step Algorithm
- Check if array length is greater than 0
- Calculate last index as length - 1
- Access the element at that index
- Print the element
Pseudocode
if array length > 0:
print arr[length - 1]
else:
print "Array is empty"
Dry Run Example
Array = [5, 15, 25, 35, 45]
Length = 5
Last Index = 4
arr[4] = 45
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(1) |
| Space Complexity | O(1) |
Language-wise Implementation
C Implementation
#include
int main() {
int arr[] = {5, 15, 25, 35, 45};
int n = sizeof(arr) / sizeof(arr[0]);
if(n > 0) {
printf("Last Element of the Array: %d", arr[n - 1]);
} else {
printf("Array is empty");
}
return 0;
}
Output
Last Element of the Array: 45
C++ Implementation
#include
using namespace std;
int main() {
int arr[] = {5, 15, 25, 35, 45};
int n = sizeof(arr) / sizeof(arr[0]);
if(n > 0)
cout << "Last Element of the Array: " << arr[n - 1];
else
cout << "Array is empty";
return 0;
}
Output
Last Element of the Array: 45
Java Implementation
public class Main {
public static void main(String[] args) {
int[] arr = {5, 15, 25, 35, 45};
if(arr.length > 0)
System.out.println("Last Element of the Array: " + arr[arr.length - 1]);
else
System.out.println("Array is empty");
}
}
Output
Last Element of the Array: 45
Python Implementation
arr = [5, 15, 25, 35, 45]
if len(arr) > 0:
print("Last Element of the Array:", arr[-1])
else:
print("Array is empty")
Output
Last Element of the Array: 45
C# Implementation
using System;
class Program {
static void Main() {
int[] arr = {5, 15, 25, 35, 45};
if(arr.Length > 0)
Console.WriteLine("Last Element of the Array: " + arr[arr.Length - 1]);
else
Console.WriteLine("Array is empty");
}
}
Output
Last Element of the Array: 45
JavaScript Implementation
let arr = [5, 15, 25, 35, 45];
if (arr.length > 0) {
console.log("Last Element of the Array:", arr[arr.length - 1]);
} else {
console.log("Array is empty");
}
Output
Last Element of the Array: 45
Common Mistakes
- Using arr[length] instead of arr[length - 1]
- Forgetting to check for empty array
- Confusing last element with largest element
- Off-by-one index errors
Summary
Finding the last element of an array reinforces the relationship between array length and indexing. Since arrays are zero-indexed, the last valid position is always one less than the total number of elements. This problem highlights the importance of boundary checks and safe memory access, which are critical when working with arrays in both low-level and high-level languages. Mastering this concept helps prevent common indexing errors and prepares you for advanced problems involving pointer manipulation, sliding windows, and stack-like behavior.
Next Problem in the Series
Count the Number of Positive and Negative Elements
