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
How to Remove Array Elements in Java | DigitalOcean
May 2, 2025 - To avoid shifting elements manually in Java, you can use System.arraycopy() to copy elements from the original array to a new array, effectively removing the element without manual shifting.
🌐
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); } }
🌐
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.
🌐
Stack Abuse
stackabuse.com › remove-element-from-an-array-in-java
Remove Element from an Array in Java
December 16, 2021 - In this tutorial, we'll showcase examples of how to remove an element from an array in Java using two arrays, ArrayUtils.remove(), a for loop and System.arraycopy().
🌐
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 - Answer: Duplicate elements from an array can be removed by using a temporary array that will count the elements one by one and only put the unique elements in the temporary array.
🌐
Stack Overflow
stackoverflow.com › questions › 57254195 › how-can-i-remove-an-element-of-array-without-using-list-or-collection-methods
java - How can I remove an element of array without using list or collection methods - Stack Overflow
For example, if you want to delete ... null... there are a few options. ... You cannot remove an object from an array without creating a new array, unless you permit unused array entries....
🌐
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. This is the reason Collection classes like ArrayList and HashSet ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 59348581 › how-can-i-remove-an-element-from-array-in-java
How can i remove an element from array in Java - Stack Overflow
To remove an element from an array in Java, you need to create a new array and copy over all the elements you want to keep.
🌐
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.
🌐
w3resource
w3resource.com › java-exercises › array › java-array-exercise-7.php
Java - Remove a specific element from an array
// Import the Arrays class from the java.util package. import java.util.Arrays; // Define a class named Exercise7. public class Exercise7 { // The main method where the program execution starts. public static void main(String[] args) { // Declare and initialize an integer array 'my_array'. int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49}; // Print the original array using Arrays.toString() method. System.out.println("Original Array : " + Arrays.toString(my_array)); // Define the index of the element to be removed (second element, index 1, value 14).
🌐
TutorialsPoint
tutorialspoint.com › home › java_data_structures › remove elements from array in java
Remove Elements from Array in Java
September 1, 2008 - To remove an existing element from an array you need to skip the element in the given position (say k) by replacing it with the next element (k+1) then, replace the element at k+1 with the element at k+2 continue this till the end of the array.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 254001 › how-would-i-go-about-removing-an-element-from-an-array
How would I go about removing an element from an array?
... You are running into two separate realities of Java arrays that a few posts skirt around: (1) arrays are fixed-size, so you cannot truly "remove" an element, and (2) if you shift elements left while scanning left-to-right, you must re-check ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-element-arraylist-java
How to remove an element from ArrayList in Java? - GeeksforGeeks
July 23, 2025 - Now, We will be discussing both ways via interpreting through a clean java program. ... There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows:
Top answer
1 of 3
3

In java when an array is initialized, it preserves the memory for that initialization which it needs to hold the data corresponding to the data type of the array declared.

So deleting an array element is something you can not do.

Simple thing that you can do is to shift the other array elements left by overriding the array element that need to be removed from array. Which means declaring new variable that holds the number of elements that the array is currently holding.

Then removing array element becomes simple as this.

  • shift elements to left starting from the element that you want to be removed.

  • decrement the variable by 1

    When you want to get the current array elements, you can simply loop through the array with reference to the no of elements variable.

2 of 3
2

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. This is the reason Collection classes like ArrayList and HashSet are very popular.

Though

Thanks to Apache Commons Utils, You can use there ArrayUtils class to remove an element from array more easily than by doing it yourself. One thing to remember is that Arrays are fixed size in Java, once you create an array you can not change their size, which means removing or deleting an item doesn't reduce the size of the array. This is, in fact, the main difference between Array and ArrayList in Java.

What you need to do is create a new array and copy the remaining content of this array into a new array using System.arrayCopy() or any other means. In fact, all other API and functions you will use do this but then you don't need to reinvent the wheel.


Copy    import java.util.Arrays;
    import org.apache.commons.lang.ArrayUtils;

     ....
    //let's create an array for demonstration purpose
    int[] test = new int[] { 101, 102, 103, 104, 105};

    System.out.println("Original Array : size : "
                           + test.length );
    System.out.println("Contents : " + Arrays.toString(test));

    // let's remove or delete an element from an Array
    // using Apache Commons ArrayUtils
    test = ArrayUtils.remove(test, 2); //removing element at index 2

    // Size of an array must be 1 less than the original array
    // after deleting an element
    System.out.println("Size of the array after
              removing an element  : " + test.length);
    System.out.println("Content of Array after
             removing an object : " + Arrays.toString(test));
🌐
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.
🌐
LeetCode
leetcode.com › problems › remove-element
Remove Element - LeetCode
Remove Element - Given an integer array nums and an integer val, remove all occurrences of val in nums in-place [https://en.wikipedia.org/wiki/In-place_algorithm]. The order of the elements may be changed.
🌐
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...