Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arraydeque
java
remove
communitycreator

What is the ArrayDeque.removeFirstOccurrence method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

An ArrayDequeArray Double Ended Queue is a growable array that allows us to add or remove an element from both the front and back.

This class is the implementation class of the Deque interface. ArrayDeque can be used as both a Stack or Queue. Null elements are prohibited.

What is the removeFirstOccurrence method of the ArrayDeque?

The removeFirstOccurrence method is used to remove the first occurrence of the specified element in the ArrayDeque object.

Syntax


public boolean removeFirstOccurrence(Object o)

Parameter

This method takes the element to be removed from the deque as a parameter.

Return value

This method returns true if the deque contains the specified element. Otherwise, false is returned.

Code

The code below demonstrates how to use the removeFirstOccurrence method.

import java.util.ArrayDeque;
class ArrayDequeRemoveFirstOccurrenceExample {
    public static void main( String args[] ) {
      ArrayDeque<String> arrayDeque = new ArrayDeque<>();
      
      arrayDeque.add("1");
      arrayDeque.add("2");
      arrayDeque.add("1");
      
      System.out.println("The arrayDeque is " + arrayDeque);
      
      System.out.println("Is element 1 present & removed: " + arrayDeque.removeFirstOccurrence("1"));
      System.out.println("The arrayDeque is " + arrayDeque);

      System.out.println("Is element 4 present & removed: " + arrayDeque.removeFirstOccurrence("1"));
      System.out.println("The arrayDeque is " + arrayDeque);
    }
}

Explanation

In the code above:

  • Line 1: We imported the ArrayDeque class.

  • Line 4: We created an ArrayDeque object with the name arrayDeque.

  • Lines 6 to 8: We used the add() method of the arrayDeque object to add three elements ("1","2","3") to deque.

  • Line 11: We used the removeFirstOccurrence method to remove the first occurrence of the element "1". The element "1" is present in two indexes, 0 and 2. After calling this method, the element at index 0 will be removed.

  • Line 15: We used the removeFirstOccurrence method to remove the first occurrence of the element "4".

    The element "4" is not present. So, this method returns false.

RELATED TAGS

arraydeque
java
remove
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring