You could use commons lang's ArrayUtils.

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

Answer from Peter Lawrey on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-array-elements
Java Remove Array Elements: Methods and Examples | DigitalOcean
May 2, 2025 - The code removes the element at index 3. This method simply copies all the elements except the one at index 3 to a new array. Unlike the previous case, this code will delete the element based on its value. This will not work with duplicates since the size of the array after deletion has to be known. package com.journaldev.java; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = new int[]{1,2,3,4,5}; int[] arr_new = new int[arr.length-1]; int j=3; for(int i=0, k=0;i<arr.length;i++){ if(arr[i]!=j){ arr_new[k]=arr[i]; k++; } } System.out.println("Before deletion :" + Arrays.toString(arr)); System.out.println("After deletion :" + Arrays.toString(arr_new)); } }
Discussions

Having trouble with java arrays, removing an element then returning a new size of an array
I’m on my phone so I’ll just explain what im thinking in pseudo code, but why don’t you just do something like: Create a new array of size n-1 where n is the size of the original array. Iterate over the old array in a for loop, adding each element to the new array. If the element at the ith position equals the element to remove, continue to the next iteration. Would that work? EDIT: I'm at my PC now so here's the code. I ended up doing it a little differently than I mentioned above. Note that it doesn't account for if the element appears multiple times in the array or when the element does not exist. public static void main(String []args) { int[] myArray = { 5,6,9,4 }; int[] newArray = removeElem(myArray, 9); for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i] + " "); } public static int[] removeElem(int[] array, int value) { int index = -1; // First confirm the element to remove exists in the array for (int i = 0; i < array.length; i++) { // If it does, store its index if (array[i] == value) index = i; } // If element exists if (index != -1) { int[] newArray = new int[array.length - 1]; for (int i = 0; i < array.length; i ++) { if (i < index) newArray[i] = array[i]; else if (i > index) newArray[i - 1] = array[i]; } return newArray; } // Do some error handling for if the element does not exist else return array; } Output is: 5 6 4 More on reddit.com
🌐 r/learnprogramming
17
4
July 13, 2018
Java - Remove element from array?
Edit: I am aware that ArrayLists are recommended, I'd love to use it but I'm not allowed to :( Archived post. New comments cannot be posted and votes cannot be cast. ... Create your account and connect with a world of communities. ... By continuing, you agree to our User Agreement and acknowledge that you understand the Privacy Policy. ... User Agreement Reddit, Inc... More on reddit.com
🌐 r/AskProgramming
1
1
January 6, 2021
How to remove numbers from an array? (java)
There are a couple of different ways that you could go about doing this. If the array is an actual array and not some other List-based array, you won't be able to straight up remove the number from the list because that will leave an empty hole in your array. You are trying to solve the problem by removing the duplicates, but I would probably recommend trying to solve the problem by creating a new array of just the unique numbers. The approach I would be to create a new variable that stores an ArrayList. Then you can loop through each element in your array. Each time you iterate through the loop, you can use ArrayList's builtin contains() method to check if the number is already in the ArrayList. If it is not, you can use the add() method to add that to the new array. After you've looped through each element in your original array, you should have a new second array with no duplicate values. If you can't keep your answer as an ArrayList and it must be an array, you can use ArrayList's toArray() method to convert it back into an array. There are other (probably more efficient) ways to solve this problem as well, but this is the first and probably easiest approach that comes to mind. More on reddit.com
🌐 r/learnprogramming
2
1
January 23, 2021
[java] How do I remove an element from an array?
You can't remove an element from an array. You can flag it and skip it (e.g., like you've done with your Integer.MIN_VALUE) but you can't remove it. You can resize the array, though, but this is expensive if you do it often. Or what's more useful is to use some sort of list where remove operations are permitted. More on reddit.com
🌐 r/learnprogramming
5
2
July 21, 2014
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - Due to the nature of array's memory placement, it is simply impossible to remove the element directly. Instead, to "remove" any element, all subsequent elements need to be shifted backward by one place. This will create an illusion that a specific element was removed.
🌐
Java67
java67.com › 2012 › 12 › how-to-remove-element-from-array-in-java-example.html
How to Remove an Element from Array in Java with Example | Java67
it is not there in java package and we ned to add dependency or add jar in classpath which is apache commonsReplyDelete ... thanks for this post, i would appreciate if u could give a hint about solving this problem (remove an element from an array) without using collections(ArrayList etc)ReplyDelete ... This is something which cant be considered as the best solution. Lets have a look on my below mentioned code: public class DeletionFromArray { public static int[] deleteArray(int arr[], int i ) { for(int j =i;j<arr.length - 1;j++) { arr[j]= arr[j+1]; } return arr; } public static void main(String[] args) { int input[] = {20, 10, 2, 4, 3}; int output[] = deleteArray(input, 1); System.out.println(output.length); for(int k=0; k<output.length - 1;k++) { System.out.println(output[k]); } } } ReplyDelete
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-an-element-at-specific-index-from-an-array-in-java
Remove an Element at Specific Index from an Array in Java - GeeksforGeeks
July 11, 2025 - Explanation: This example includes finding the element at the specified index and then removing that element. The rest of the elements are copied into a new array. This would lead to an array of size one less than the original array. We can use Java 8 streams to remove element at specific index from an array, when modifying small or mid-sized arrays.
🌐
Baeldung
baeldung.com › home › java › java array › removing an element from an array in java
Removing an Element from an Array in Java | Baeldung
June 12, 2024 - Usually, this approach creates a brand-new array and copies all the values except for the value being removed. We can use the Apache Commons Lang library to remove the given element of an array.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › remove/delete an element from an array in java
Remove/Delete An Element From An Array In Java
April 1, 2025 - Java arrays do not provide a direct remove method to remove an element. In fact, we have already discussed that arrays in Java are static so the size of the arrays cannot change once they are instantiated.
Find elsewhere
🌐
Netjstech
netjstech.com › 2017 › 07 › how-to-remove-elements-from-array-java.html
How to Remove Elements From an Array Java Program | Tech Tutorials
Enter Element to be deleted : 5 Elements -- 1 2 12 7 3 8 Enter Element to be deleted : 8 Elements -- 1 2 5 12 7 3 · With copying the element to the new array problem of empty space is solved. If you can use Apache commons in your application then there is a utility class ArrayUtils that can be used to remove elements from an array. import java.util.Scanner; import org.apache.commons.lang3.ArrayUtils; public class ElemRemoval { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] intArr = {1, 2, 5, 12, 7, 3, 8}; System.out.print("Enter Element to be deleted : "); int elem = in.nextInt(); for(int i = 0; i < intArr.length; i++){ if(intArr[i] == elem){ // Using ArrayUtils intArr = ArrayUtils.remove(intArr, i); break; } } System.out.println("Elements -- " ); for(int i = 0; i < intArr.length; i++){ System.out.print(" " + intArr[i]); } } }
🌐
Reddit
reddit.com › r/learnprogramming › having trouble with java arrays, removing an element then returning a new size of an array
r/learnprogramming on Reddit: Having trouble with java arrays, removing an element then returning a new size of an array
July 13, 2018 -

So guys i'm having trouble finding a solution for this question. I'm kinda frustrated that i didn't solve this kind of problem during my exam for employment.

Problem: The task is to provide an implementation for the given function. int[] removeElem(int[] array, value), the objective is to find if the array contains the given 'value' inside it. if found then remove the element from the array then return an array with a new size, else return original array.

So far this is what i got:

  public static void main(String[] args) {
  int[] myArray = {5,6,9,4};
  
  int[] newArray = removeElem(myArray,9);
  
  for(int i=0;i<newArray.length;i++){
      System.out.print(newArray[i] + " ");
  }

}

public static int[] removeElem(int[] array, int value){
    int[] tempArray = array;
    int newArrayLength = 0;
    int[] newArray;
    
    for(int index=0;index < tempArray.length; index++){
        if(tempArray[index] == value){
            tempArray[index] = -1;
            newArrayLength++;
        }
    }
    
    newArray = new int[tempArray.length - newArrayLength];
    for(int index=0;index < newArray.length; index++){
        if(tempArray[index] != -1){
            newArray[index] = tempArray[index];
        }
    }
    
    return newArray;
    
}

Unfortunately i can't get it right :/

Edit: Thank you! everyone for suggesting different solutions to this problem, I feel like I need to work more on my solutions and such :|.

Top answer
1 of 5
2
I’m on my phone so I’ll just explain what im thinking in pseudo code, but why don’t you just do something like: Create a new array of size n-1 where n is the size of the original array. Iterate over the old array in a for loop, adding each element to the new array. If the element at the ith position equals the element to remove, continue to the next iteration. Would that work? EDIT: I'm at my PC now so here's the code. I ended up doing it a little differently than I mentioned above. Note that it doesn't account for if the element appears multiple times in the array or when the element does not exist. public static void main(String []args) { int[] myArray = { 5,6,9,4 }; int[] newArray = removeElem(myArray, 9); for (int i = 0; i < newArray.length; i++) System.out.print(newArray[i] + " "); } public static int[] removeElem(int[] array, int value) { int index = -1; // First confirm the element to remove exists in the array for (int i = 0; i < array.length; i++) { // If it does, store its index if (array[i] == value) index = i; } // If element exists if (index != -1) { int[] newArray = new int[array.length - 1]; for (int i = 0; i < array.length; i ++) { if (i < index) newArray[i] = array[i]; else if (i > index) newArray[i - 1] = array[i]; } return newArray; } // Do some error handling for if the element does not exist else return array; } Output is: 5 6 4
2 of 5
2
newArray[index] = tempArray[index]; This is your problem. As you skip elements that are to be removed, the insertion index in newArray must no longer match the read index in tempArray, but you're using the same index for both. You need to use two indices, and only increment the newArray index when you insert an element, while incrementing the tempArray index every time. Also the whole thing would be better if you just used Lists or Streams, but maybe your interviewer didn't want that. It's really best to try to avoid using raw arrays in Java.
🌐
CodeScracker
codescracker.com › java › program › java-program-delete-element-from-array.htm
Java Program to Remove an Element from an Array
Here is its sample run with user input 6 as size, 20, 21, 20, 22, 20, 23 as six elements, and 20 as element to delete: In above program, the following block of for loop: for(i=0; i<size; i++) { if(element==arr[i]) { for(j=i; j<(size-1); j++) arr[j] = arr[j+1]; size--; i--; count++; } } can also ...
🌐
Codidact
software.codidact.com › posts › 289778
How do I remove an element from a Java array? - Software Development
We welcome questions about all ... to review? Please join us. ... One way to remove element from array is to replace element with zero....
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Remove element from an Array Java - Examples Java Code Geeks - 2026
July 6, 2022 - Approach 1 talks about a mature way of deleting an element from an array. Let us understand this with the help of a simple code snippet. ... If everything goes well, the element present at index=2 will be removed from the specified array.To find out more about the best way to copy an array for each possible case you can check the Java Copy Array Example
🌐
CODEDOST
codedost.com › home › java › arrays in java › java program to delete an element from an array
JAVA program to delete an element from an array | CODEDOST
July 15, 2018 - Hence our new array would be a={7,8,12,9}. import java.util.*; class arr4 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i,n,pos; System.out.println("Enter the number of elements:") ; n = sc.nextInt(); int[] a = new int[n]; System.out.println("Enter the elements") ; for(i=0;i<n;i++) { a[i] = sc.nextInt(); } System.out.println("Enter the position of the number which is to be deleted"); pos = sc.nextInt(); for(i=pos;i<n-1;i++) { a[i]=a[i+1]; } n=n-1; System.out.println("\nOn deleting new array we get is\n"); for(i=0;i<n;i++) { System.out.println("a["+i+"] = "+a[i]); } } }
🌐
w3resource
w3resource.com › java-exercises › array › java-array-exercise-7.php
Java - Remove a specific element from an array
May 9, 2025 - System.out.println("Original Array : " + Arrays.toString(my_array)); // Define the index of the element to be removed (second element, index 1, value 14). int removeIndex = 1; // Loop to remove the element at the specified index. for (int i = removeIndex; i < my_array.length - 1; i++) { my_array[i] = my_array[i + 1]; } // Print the modified array after removing the second element. System.out.println("After removing the second element: " + Arrays.toString(my_array)); } } ... Original Array : [25, 14, 56, 15, 36, 56, 77, 18, 29, 49] After removing the second element: [25, 56, 15, 36, 56, 77, 18, 29, 49, 49] ... Write a Java program to remove all occurrences of a given value from an array.
🌐
TutorialsPoint
tutorialspoint.com › java_data_structures › java_data_structures_remove_elements_from_array.htm
Remove Elements from Arrays
public class RemovingElements { ... of the array after deletion :: 10, 20, 30, 96, 66, The ArrayUtils class provide remove() method to delete an element from an array. import java.util.Scanner; import org.apache.commons.l...
🌐
Techcrashcourse
techcrashcourse.com › 2018 › 11 › java-program-to-delete-element-from-array.html
Java Program to Delete an Element from Array at Given Position
Let inputArray is an array of length N, and we want to delete an element at index I. Shift all elements from inputArray[I+1] to inputArray[M-1] to previous index. Move inputArray[j] to inputArray[j - 1], I+1 <= j <= M-1. Above step delete inputArray[I] by overwriting it with inputArray[I+1]. ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_remove.asp
Java ArrayList remove() Method
If a value is specified and multiple elements in the list have the same value then only the first one is deleted. If the list contains integers and you want to delete an integer based on its value you will need to pass an Integer object. See More Examples below for an example. ... T refers to the data type of items in the list. Remove an integer from the list by position and by value: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(5); list.add(8); list.add(9); list.add(1); list.remove(Integer.valueOf(1)); // Remove by object list.remove(1); // Remove by index System.out.println(list); } }
🌐
Sanfoundry
sanfoundry.com › java-program-delete-specified-integer-from-array
Java Program to Delete an Element from an Array - Sanfoundry
May 23, 2022 - This is a Java Program to Delete the Specified Integer from an Array. Enter size of array and then enter all the elements of that array. Now enter the element you want to delete. We first find the location of that element and then shift the ...
🌐
Educative
educative.io › answers › how-to-retrieve-update-and-delete-an-element-in-a-java-array
How to retrieve, update and delete an element in a Java array
For example, to update the first element of myArray to the value of the variable newValue, we can use: ... We can delete an element from an array by setting the value of the item to null.