Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

ArrayList and LinkedList remove() methods in Java with Examples

Last Updated on March 9, 2022 by Ria Pathak

Introduction

The linked list is one of the most crucial concepts and data structures to grasp while preparing for interviews. In a coding interview, having a thorough understanding of Linked Lists might be a major benefit.

The remove() method has two versions in Java’s List interface (which is implemented by ArrayList and LinkedList).

Approach and Algorithm (remove(Object x))

Syntax: boolean remove(Object x)

It accepts the object that is to be removed from the list.

  • If it discovers and removes the element, it returns true.
  • If the element to be removed isn’t present, it returns false.

If the element is present, it removes the first occurrence of the specified element from the given list. The specified list is not altered if the entry is not present.

After eliminating an element, it shifts any remaining items to the left and decreases their indexes by one.

Input

[Coding, is, Fun], Element to be removed – “is”

Output

[Coding, Fun]

Explanation – As we can see, the element to be removed from the list was “is”, and it has been removed from the given list.

Exceptions Thrown

  • If the type of the supplied element is incompatible with this collection, a ClassCastException will be thrown.
  • If the provided element is null and this collection does not allow null elements, a NullPointerException will be thrown.
  • If the remove operation is not supported by this collection, an UnsupportedOperationException will be thrown.

Code Implementation

import java.util.*;
 
public class PrepBytes
{
    public static void main(String[] args)
    {

        List  myAlist = new ArrayList();
        myAlist.add("Coding");
        myAlist.add("is");
        myAlist.add("Fun");
        System.out.println("Original ArrayList : " + myAlist);
        myAlist.remove("is");
        System.out.println("Modified ArrayList : " + myAlist);
        
         List  myLlist = new LinkedList();
        myLlist.add("Coding");
        myLlist.add("is");
        myLlist.add("Fun");
        System.out.println("Original LinkedList : " + myLlist);
        myLlist.remove("is");
        System.out.println("Modified LinkedList : " + myLlist);

       
    }
}

Output

Original ArrayList : [Coding, is, Fun]
Modified ArrayList : [Coding, Fun]
Original LinkedList : [Coding, is, Fun]
Modified LinkedList : [Coding, Fun]

Time Complexity: O(n), as we have to traverse the list and find the element to be removed.
Space Complexity: O(1), as only temporary variables are being created.

Approach and Algorithm ( remove(int index) )

Syntax: object remove(int index)

It deletes the element at the specified index. The item that was removed is returned.

After eliminating an element, it shifts any remaining items to the left and decreases their indexes by one.

If the list contains integer types, then we have to call the list.remove(new Integer(value)) so that the remove() method recognizes that we want to remove element by value and not by index.

Input

[Coding, is, Fun], Index at which the element will be removed = 1

Output

[Coding, Fun]

Explanation

As we can see, the element at index 1 was "is", so “is” was removed from the given list.

Exceptions Thrown

  • If the index is out of bounds, it throws IndexOutOfBoundsException.

Code Implementation

import java.util.*;
 
public class PrepBytes
{
    public static void main(String[] args)
    {

        List myAlist = new ArrayList();
        myAlist.add("Coding");
        myAlist.add("is");
        myAlist.add("Fun");
        System.out.println("Original ArrayList : " + myAlist);
        myAlist.remove(1);
        System.out.println("Modified ArrayList : " + myAlist);
 
        List myLlist = new LinkedList();
        myLlist.add("Coding");
        myLlist.add("is");
        myLlist.add("Fun");
        System.out.println("Original LinkedList : " + myLlist);
        myLlist.remove(1);
        System.out.println("Modified LinkedList : " + myLlist);

    }
}

Output

Original ArrayList : [Coding, is, Fun]
Modified ArrayList : [Coding, Fun]
Original LinkedList : [Coding, is, Fun]
Modified LinkedList : [Coding, Fun]

Time Complexity: O(n), as we have to traverse the list till the specified index.
Space Complexity: O(1), as only temporary variables are being created.

Important Points

  • Because the size of the array is predetermined, so there is no straightforward way to delete elements from it. As a result, there are no add(), remove(), or delete() functions. These methods, however, are available in Collections such as ArrayList and Hashset. When we knew that we will be needing these methods, it’s best to transform an array to an ArrrayList or use an ArrayList from the start.
  • When iterating through elements, it is not suggested to use the list interface’s remove() method. ConcurrentModificationException may be thrown. It is suggested to utilize the Iterator.remove() function when iterating over elements.

So, in this article, we tried to explain how to use the ArrayList and LinkedList remove() methods in Java. It is an important concept when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Leave a Reply

Your email address will not be published. Required fields are marked *