Java ArrayList lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

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
CallWhat 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

ParameterDescription
o (Object)The value to find. May be null.

Return Value

Return ValueDescription
intThe index of the last element equal to o, or -1 if there is none.

Compare with Similar Methods

Related search methods:

MethodHow it differs
indexOf(o)Searches from the front and returns the first match.
contains(o)Only answers whether a match exists.
remove(o)Always removes the first match; remove(lastIndexOf(o)) removes the last.
get(i)Reads the element at the index you found.

Java Examples

Example 1 — Find the Most Recent Visit

A visit log in time order: find each visitor's first and last appearance.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListLastIndexOfDemo {
    public static void main(String[] args) {
        List<String> visits = new ArrayList<>(Arrays.asList("Leo", "Mia", "Leo", "Zoe", "Leo"));

        System.out.println("first Leo: " + visits.indexOf("Leo"));
        System.out.println("last Leo:  " + visits.lastIndexOf("Leo"));
        System.out.println("last Mia:  " + visits.lastIndexOf("Mia"));
        System.out.println("last Ana:  " + visits.lastIndexOf("Ana"));
    }
}

How It Works

  1. Three visits by Leo. He appears at indexes 0, 2, and 4.
  2. From the end. lastIndexOf starts at index 4 and matches Leo immediately.
  3. Single match. Mia appears once, so her last index equals her first, 1.
  4. 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);
    }
}

How It Works

  1. Two searches per value. indexOf finds the earliest position of each number and lastIndexOf the latest.
  2. Different means repeated. For 3 the two answers are 0 and 6, and for 5 they are 3 and 5.
  3. Unique values. 8 and 9 appear once, so both searches return the same index and they are skipped.
  4. 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);
    }
}

How It Works

  1. Locate the last copy. lastIndexOf("x") returns 2.
  2. Remove by index. remove(i) with an int deletes exactly that position and leaves the earlier x alone.
  3. Guarded. The -1 check avoids calling remove(-1) when the value is missing.
  4. 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).
Yes, just like indexOf() and contains().

Next: listIterator()

Iterate both ways and edit while looping.

listIterator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful