Java ArrayDeque removeLastOccurrence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, removeLastOccurrence(o) searches an ArrayDeque from the tail toward the head and removes the last element that equals o. It returns true if an element was removed and false otherwise.

It mirrors removeFirstOccurrence(o). When a value appears only once, both methods remove the same element; they differ only when there are several copies.

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.

Syntax

Pass the value to remove.

Java
Deque<String> deque = new ArrayDeque<>(Arrays.asList("a", "b", "a"));
boolean removed = deque.removeLastOccurrence("a");   // true, [a, b]
CallWhat it does
deque.removeLastOccurrence(o)Removes the match nearest the tail.
deque.removeFirstOccurrence(o)Removes the match nearest the head.
deque.removeLast()Removes whatever is at the tail, no search.
deque.removeLastOccurrence(null)Returns false.

There is no shorter alias for this method. Like its head-side twin, it searches linearly and returns false for null.

Library declarations
Java
// java.util.ArrayDeque<E>
public boolean removeLastOccurrence(Object o)

// Declared in Deque<E>

Parameters

ParameterDescription
oRequired. The value to remove, compared with o.equals(element). null never matches.

Return Value

Return ValueDescription
booleantrue if an element was removed; false if no element matched.

Compare with Similar Methods

How the "remove by value" methods choose what to remove:

MethodHow it differs
removeFirstOccurrence(o)The copy nearest the head.
remove(o)Same as removeFirstOccurrence.
removeLast()The tail element, whatever its value.
removeIf(filter)Every matching element at once.

Java Examples

Example 1 — Remove the Last Matching Element

Two elements equal "a"; the one nearer the tail is removed.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeRemoveLastOccDemo {
    public static void main(String[] args) {
        Deque<String> d = new ArrayDeque<>(Arrays.asList("a", "b", "a", "c"));

        boolean removed = d.removeLastOccurrence("a");

        System.out.println(removed);
        System.out.println(d);
    }
}

How It Works

  1. Search from the tail. The search starts at c and moves toward the head.
  2. Last copy found. The "a" in the third position is the first match seen from that side, so it is removed.
  3. Head copy stays. The "a" at the head is untouched, giving [a, b, c].
  4. Result. The method returns true.

Example 2 — removeFirstOccurrence vs removeLastOccurrence

Copy one deque, then remove the value 2 from the front side in one copy and from the back side in the other.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeFirstVsLastOcc {
    public static void main(String[] args) {
        Deque<Integer> a = new ArrayDeque<>(Arrays.asList(1, 2, 1, 2));
        Deque<Integer> b = new ArrayDeque<>(a);

        a.removeFirstOccurrence(2);
        b.removeLastOccurrence(2);

        System.out.println("first: " + a);
        System.out.println("last:  " + b);
    }
}

How It Works

  1. Two equal deques. new ArrayDeque<>(a) copies a, so both start as [1, 2, 1, 2].
  2. Front-side removal. In a, the 2 in the second position goes, leaving [1, 1, 2].
  3. Back-side removal. In b, the 2 at the tail goes, leaving [1, 2, 1].
  4. Boxing again. The int argument 2 is boxed to an Integer and compared with equals.

Example 3 — Undo the Most Recent Matching Action

An action log grows at the tail. Undoing "like:42" should remove the newest one, not the oldest.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeUndoLike {
    public static void main(String[] args) {
        Deque<String> log = new ArrayDeque<>(
            Arrays.asList("like:42", "like:7", "share:42", "like:42"));

        boolean undone = log.removeLastOccurrence("like:42");
        System.out.println(undone + " " + log);

        System.out.println(log.removeLastOccurrence("like:99") + " " + log.size());
    }
}

How It Works

  1. Newest at the tail. The log has two like:42 entries; the newer one is last.
  2. Undo the newest. removeLastOccurrence removes the tail entry, so the older like stays in the history.
  3. Printed together. true and the shorter log appear on one line through string concatenation.
  4. Nothing to undo. like:99 is not in the log, so the call returns false and the size stays 3.

Frequently Asked Questions

It removes the last element (searching from the tail) that equals the argument, and returns true if one was removed.
removeFirstOccurrence removes the copy nearest the head; removeLastOccurrence removes the copy nearest the tail.
No, only one. Loop until it returns false, or use removeIf().
false. ArrayDeque cannot contain null, and no exception is thrown.
No. removeLast() removes the tail element whatever it is; removeLastOccurrence(o) searches for a specific value.

Next: retainAll()

Keep only the elements that are in another collection.

retainAll() 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.

11 people found this page helpful