Java program to Remove element from array

In this post, we will see how to remove an element from array in java.
Unlike Arraylist,Java Arrays class does not provide any direct method to add or delete element. As Array is fixed size in nature, you can not shrink or grow it dynamically. You need to create new array and copy all elements except the element which you want to remove.

If you have no duplicates in array, you can simply use Apache common’s ArrayUtil class.It has removeElement method which removes first occurence of the element from the array and return new array.

Remove element from array with inbuilt functon

You can also use Apache common’s ArrayUtils.removeElement(array, element) method to remove element from array. It will remove first occurence of element in the array.It is cleaner and elegant way to remove any element from array.
Step 1: Create a simple java maven project.
Step 2: Add Apache common dependency to pom.xml.

Step 3: Create a class named "RemoveElementApacheCommonMain"

When you run above program, you will get below output:

Before removing element
1 34 2 23 35 65
============
After removing element 23
1 34 2 35 65

Remove element from array without inbuilt functon

You are given an array and element.You need to remove all the occurrences of the element in the array and return its new length.
You should not use any extra space and without inbuilt function

If you are not allowed to use any inbuild function, you can use below algorithm.

  • Let’s say your array is arr[] and length=0
  • Iterate over array and if element is equal to arr[i], skip the iteration(as we need to remove the element) else increment the length and assign current element.
  • When i reaches to last element, you will get newLength and all instances of element will be removed.

Let’s see with the help of simple program.

Create a class named "RemoveElementMain".

When you run above program, you will get below output:

Before removing element
1 34 23 2 23 35 65
============
After removing element 23
1 34 2 35 65

That’s all about removing element from array in java.

Was this post helpful?

Related Posts

  • 17 May

    How to Initialize an Array with 0 in Java

    Table of ContentsArraysInitializing Newly Created ArrayUsing Default Initialization of Arrays in JavaUsing Initialization After DeclarationUsing Simultaneous Initializing and Declaration of ArrayUsing Reflection in JavaInitializing Existing ArrayUsing the Arrays.fill() Function of JavaUsing the for LoopUsing Reassignment From Another ArrayUsing Collections.nCopies() Function of JavaInitializing User-Defined Object ArrayConclusionWas this post helpful? This article discusses the arrays and different […]

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

  • Check if array is empty in Java
    04 March

    Check if Array Is Empty in Java

    Table of ContentsCheck if the Array Is Empty in JavaThe Array Variable Has the Null ReferenceThe Array Does Not Contain Any ElementThe Array Has Only Null ElementsUsing the Java Library to Check if the Array Is Empty in JavaUsing Apache Commons Library to Check if the Array Is Empty in JavaConclusionWas this post helpful? In […]

  • Initialize empty array in java
    01 January

    Initialize empty array in java

    Table of ContentsIntroductionHow do you initialize an empty array in java?Using new Keyword with predefined Values and SizeUsing Anonymous Array Objects to initialize empty arrayUsing java.util.Scanner Class for user input with predefined size. Using java.io.BufferedReader to initialize array for user input with unknown sizeUsing fill() method of java.util.Arrays Class to initialize empty arrayWas this post helpful? […]

  • Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers
    07 April

    Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

    In this article, we look at a problem : Given an Array of Positive Integers, Find the [Maximum Difference](https://java2blog.com/maximum-difference-between-two-elements-in-array/ "Maximum Difference") between Two Adjacent Numbers. For each pair of elements we need to compute their difference and find the Maximum value of all the differences in array. Let us look at an example, Consider this […]

  • 11 November

    How to print array in java

    Learn about how to print array in java in multiple ways.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.