The java.util.ArrayDeque.removeLastOccurrence(Object) method in Java is used to remove the last occurrence of a specific element from this deque. If the element is present only once, then that element is removed and true is returned. If no such element is removed then false is returned.
Syntax:
Array_Deque.removeLastOccurrence(Object O)
Parameters: The method takes one parameter O of ArrayDeque type which refers to the element whose last occurrence is to be removed.
Return Value: This method returns true if the element is present in the Deque else it returns false.
Below programs illustrate the Java.util.ArrayDeque.removeLastOccurrence() method:
Program 1:
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
Deque<String> de_que = new ArrayDeque<String>();
de_que.add( "Welcome" );
de_que.add( "To" );
de_que.add( "Geeks" );
de_que.add( "For" );
de_que.add( "Geeks" );
System.out.println( "Initial ArrayDeque: " + de_que);
de_que.removeLastOccurrence( "Geeks" );
de_que.removeLastOccurrence( "For" );
System.out.println( "ArrayDeque after removing "
+ "elements: " + de_que);
}
}
|
Output:
Initial ArrayDeque: [Welcome, To, Geeks, For, Geeks]
ArrayDeque after removing elements: [Welcome, To, Geeks]
Program 2:
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
Deque<Integer> de_que = new ArrayDeque<Integer>();
de_que.add( 10 );
de_que.add( 15 );
de_que.add( 30 );
de_que.add( 20 );
de_que.add( 5 );
System.out.println( "Initial ArrayDeque: " + de_que);
de_que.removeLastOccurrence( 30 );
de_que.removeLastOccurrence( 5 );
System.out.println( "ArrayDeque after removing "
+ "elements: " + de_que);
}
}
|
Output:
Initial ArrayDeque: [10, 15, 30, 20, 5]
ArrayDeque after removing elements: [10, 15, 20]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Dec, 2018
Like Article
Save Article