Java LinkedList removeFirstOccurrence() Method with Examples

In the previous article, we have discussed about Java LinkedList removeLastOccurrence() Method with Examples

In this article we are going to see the use of Java LinkedList removeFirstOccurrence() method along with suitable examples.

Java LinkedList removeFirstOccurrence() Method with Examples

This java.util.LinkedList.removeFirstOccurrence() method is used to remove the firstt occurrence of a specified element from the LinkedList which means if same element is present at multiple positions of the list then from the first position the respective element gets removed. It returns true if that specific element is present and got removed else the list remains unchanged.

Syntax:

LinkedListName.removeFirstOccurrence(Object o)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Object o refers to the specified element from the LinkedList which is to be removed from first occurrence.

Let’s see removeFirstOccurrence() method with suitable examples.

Example-1: Java LinkedList removeFirstOccurrence() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the first occurrence of the specific element from the LinkedList using removeFirstOccurrence(Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("ram");
        l.add("shyam");
        l.add("gyan");
        l.add("ram");
        l.add("gyan");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the 1st occurrence of 'gyan' from the LinkedList
        l.removeFirstOccurrence("gyan");
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [ram, shyam, gyan, ram, gyan]
The new elements of LinkedList are: [ram, shyam, ram, gyan]

Example-2:Java LinkedList removeFirstOccurrence() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements
  • Then remove the first occurrence of the specific element from the LinkedList using removeFirstOccurrence(Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(4);
        l.add(6);
        l.add(4);
        l.add(2);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the 1st occurrence of 4 from the LinkedList
        l.removeFirstOccurrence(4);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 4, 6, 4, 2]
The new elements of LinkedList are: [2, 6, 4, 2]

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: