You could use commons lang's ArrayUtils.

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

Answer from Peter Lawrey on Stack Overflow
🌐
Netjstech
netjstech.com › 2017 › 07 › how-to-remove-elements-from-array-java.html
How to Remove Elements From an Array Java Program | Tech Tutorials
When you remove an element from an array, you can fill the empty space with 0, space or null depending on whether it is a primitive array, string array or an Object array. Other alternative is to create a new array and copy the elements in that array. New array should have size of old array’s ...
Discussions

Removing an item from an array
Hello sun forum, I wonder if anyone would be kind enough to assist a newbie Java hobbyist with a few basic questions? I don't want to take up too much of your time so I will be brief. I want to pro... More on forums.oracle.com
🌐 forums.oracle.com
November 17, 2007
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
Removing an item from an array list
The problem is in this part: for (Student i : roster) { int sId = i.getId(); if (id == sId) { roster.remove(id); } else if (id != sId) { System.out.println("ERROR: Invalid student ID.\n"); } } You can't remove items from the list while you're looping through it with this for loop. If you want to use a loop, you should use this kind of loop: for (int i = 0; i < roster.size(); ++i) { Student s = roster.get(i); int sId = s.getId(); if (id == sId) { roster.remove(i); i--; // This is important! } } See, this lets you remove the student at the current index. However, as you remove it, you have to decrement the index, otherwise you'll skip the next item in the list. More on reddit.com
🌐 r/learnjava
8
5
March 31, 2017
A problem with java arraylist remove method
you can't remove an item from a collection you're iterating over. You can get around this by explicitly using an Iterator and removing the item More on reddit.com
🌐 r/learnjava
7
4
June 1, 2018
🌐
Oracle
forums.oracle.com › ords › apexds › post › removing-an-item-from-an-array-7950
Removing an item from an array
November 17, 2007 - Hello sun forum, I wonder if anyone would be kind enough to assist a newbie Java hobbyist with a few basic questions? I don't want to take up too much of your time so I will be brief. I want to pro...
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - The final argument is the number of elements to copy from the source array. The arraycopy is generally used to copy contents from some source array into some destination array. Though, we can also copy an array, or a part of it, into itself. This allows us to shift a part of it to the left like last time: int[] array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int index = 3; To remove the element, we only need to write this one line of code:
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-remove-array-elements
How to Remove Array Elements in Java | DigitalOcean
May 2, 2025 - You can’t directly remove elements from arrays in Java because arrays are fixed-size, meaning their size is determined at the time of creation and cannot be changed later.
Find elsewhere
🌐
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
... There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove(), or search an element in Array.
🌐
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); } }
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › home › java_data_structures › remove elements from array in java
Remove Elements from Array in Java
September 1, 2008 - Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm to delete an element available at the Kth position of LA. Step 1 - Start Step 2 - Set J = K Step 3 - Repeat steps 4 and 5 while J < N Step 4 - Set LA[J] = LA[J + 1] Step 5 - Set J = J+1 Step 6 - Set N = N-1 Step 7 - Stop · public class RemovingElements { public static void main(String args[]) { int[] myArray = {10, 20, 30, 45, 96, 66}; int pos = 3; int j = myArray.length; for(int i = pos; i < j-1; i++) { myArray[i] = myArray[i+1]; } System.out.println("Contents of the array after deletion ::"); for(int i = 0; i < myArray.length-1; i++) { System.out.print(myArray[i]+ ", "); } } }
🌐
Quora
quora.com › How-does-one-remove-an-element-from-an-array-in-Java
How does one remove an element from an array in Java? - Quora
Answer (1 of 10): Removing an element from an array is a cumbersome effort if the order matters in your array. Otherwise, it is really easy. Let me explain both ways. Order of the elements does NOT matter Let’s assume your have a partially filled array [5, 9, 6, 8, 0, 0, 0] (0 means don’t ...
🌐
Codidact
software.codidact.com › posts › 289778
How do I remove an element from a Java array? - Software Development
One way to remove element from array is to replace element with zero. Below is a rough snippet I am sharing: ```java int N = sc.nextInt(); int pos...
🌐
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 - Arrays have a fixed size, so creating a new array without the target element is often necessary. ... The basic approach to remove an element at a specific index is using a loop. ... // Java program to remove an element // from a specific index ...
🌐
Programiz
programiz.com › java-programming › library › arraylist › remove
Java ArrayList remove()
Become a certified Java programmer. Try Programiz PRO! ... The remove() method removes the single element from the arraylist.
🌐
Java Guides
javaguides.net › 2024 › 05 › java-remove-element-from-array.html
Java: Remove Element from Array
May 29, 2024 - This guide will cover different ways to remove an element from an array, including using loops, the System.arraycopy method, and converting the array to a list and back to an array. ... In Java, arrays are fixed-size data structures that store elements of the same type.
🌐
W3Docs
w3docs.com › java
Removing an element from an Array in Java
public class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3}; // Removing the element at index 1 int[] newNumbers = new int[numbers.length - 1]; System.arraycopy(numbers, 0, newNumbers, 0, 1); System.arraycopy(numbers, 2, newNumbers, 1, numbers.length - 2); } }
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › arraylist removeif(): remove elements matching a condition
ArrayList removeIf(): Remove Elements Matching a Condition
August 7, 2023 - Java ArrayList.removeIf() method removes all elements that satisfy a condition by iterating through the elements of the current arraylist and matching them against the condition specified by the argument Predicate.
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-all-occurrences-of-an-element-from-array-in-java
Remove all Occurrences of an Element from Array in Java - GeeksforGeeks
July 11, 2025 - Problem Stament: Given an array and a key, the task is to remove all occurrences of the specified key from the array in Java. ... Input: array = { 3, 9, 2, 3, 1, 7, 2, 3, 5 }, key = 3 Output: [9, 2, 1, 7, 2, 5] Input: array = { 10, 20, 10, 30, 50, 10 }, key = 10 Output: [20, 30, 50] Illustration: In this example, we will remove all occurrences of an element from an array using the Arrays.copyOf method.