In Java, lastIndexOf(o) searches an ArrayList from the end and returns the index of the last element equal to o, or -1 if there is none.
The returned index still counts from the front (0 is the first element). When a value appears only once, indexOf and lastIndexOf return the same number, which gives a quick way to check for duplicates.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.
Syntax
Pass the value to find; check the result for -1.
Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "a"));
int last = list.lastIndexOf("a"); // 2
int first = list.indexOf("a"); // 0
int none = list.lastIndexOf("z"); // -1
Call
What it does
list.lastIndexOf(o)
Index of the last match, or -1.
list.indexOf(o)
Index of the first match, or -1.
list.indexOf(o) != list.lastIndexOf(o)
true if o appears more than once.
list.subList(0, end).lastIndexOf(o)
Searches backwards only among indexes below end.
The search starts at the last element and moves toward index 0, stopping at the first match it meets. Its running time grows with the list size.
Library declarations
Java
// java.util.ArrayList<E>
public int lastIndexOf(Object o)
// Declared in List<E>
Parameters
Parameter
Description
o (Object)
The value to find. May be null.
Return Value
Return Value
Description
int
The index of the last element equal to o, or -1 if there is none.
Three visits by Leo. He appears at indexes 0, 2, and 4.
From the end.lastIndexOf starts at index 4 and matches Leo immediately.
Single match. Mia appears once, so her last index equals her first, 1.
Not found. Ana never visited, so the result is -1.
Example 2 — Spot Values That Appear More Than Once
Compare indexOf and lastIndexOf for each value to find the repeated ones.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListLastIndexOfDupes {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(Arrays.asList(3, 8, 3, 5, 9, 5, 3));
List<Integer> repeated = new ArrayList<>();
for (Integer n : nums) {
if (nums.indexOf(n) != nums.lastIndexOf(n) && !repeated.contains(n)) {
repeated.add(n);
}
}
System.out.println("repeated: " + repeated);
}
}
Output
repeated: [3, 5]
How It Works
Two searches per value.indexOf finds the earliest position of each number and lastIndexOf the latest.
Different means repeated. For 3 the two answers are 0 and 6, and for 5 they are 3 and 5.
Unique values. 8 and 9 appear once, so both searches return the same index and they are skipped.
Report each once. The !repeated.contains(n) check stops 3 and 5 from being added again when they come up later in the loop.
Example 3 — Remove the Last Occurrence of a Value
Use lastIndexOf with remove(int), then compare with remove(Object).
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListRemoveLastCopy {
public static void main(String[] args) {
List<String> items = new ArrayList<>(Arrays.asList("x", "y", "x", "z"));
int i = items.lastIndexOf("x");
if (i != -1) {
items.remove(i);
}
System.out.println("last x removed: " + items);
items.remove("x");
System.out.println("first x removed: " + items);
}
}
Output
last x removed: [x, y, z]
first x removed: [y, z]
How It Works
Locate the last copy.lastIndexOf("x") returns 2.
Remove by index.remove(i) with an int deletes exactly that position and leaves the earlier x alone.
Guarded. The -1 check avoids calling remove(-1) when the value is missing.
Compare with remove(Object).remove("x") always deletes the first match, which is the x at index 0.
Frequently Asked Questions
The index of the last element equal to the argument, or -1 if no element matches.
No. It searches from the end, but the index it returns is counted from the front, like every list index.
Compare list.indexOf(x) with list.lastIndexOf(x). If they differ, x appears at least twice.
When the value appears exactly once (same index), or not at all (both -1).