Removing an element from the java array is done by locating the item index and shifting the rest of the items to the index position. The Java array can’t be modified. We can’t remove a specific item from the Java array. The alternative is to shift the rest of the items to the index position.

In Java, you can remove a specific item from an array using a range of techniques. The Java array is fixed in length. Removing a specific item from an array is nothing but shifting the rest of the items to an index position. Here, we’ll see how to remove a specific item from a Java array.

The specific item can be added as a duplicate to the array. In the first occurrence, you can remove a specific item from the array, or in any event remove a specific item from the array. In this post, we’re going to explore the possibility of seeing both types. If you are confident that the element will be inserted once, you can use the remove once logic in your code.

There are several ways to remove a specific item from the Java array. Java 8 supports writing a lightweight code using streams. There are other methods you can use depending on the size of the array. For example, making a duplicate array is not recommended if the size of the array is high.



Remove a specific item from an array

The removal of a specific item from the array involves two steps in java. The first step is to identify the index of the array in which the specific items are located. The second step is to delete the specific item by shifting the rest of the items to one index position. The last index is marked as empty.

  • Using Java 8 Streams
  • Using one array
  • Using two array
  • Using the system array copy
  • Using array list


Solution 1 – Using Java 8

Java 8 supports the writing of a lightweight code using streams. You may delete a specific item from an array using java streams. The array is transformed as a stream, filtering a specific item, and generating a new array without a specific item. The example below illustrates how to delete a particular item from a Java 8 array.

In the example below, the IntStream loops the array using the length of the array and filters the index that matches the given element in the array. The IntStream loops the array again and copies the element to another array, excluding the index already found. The element can be removed from the array in this way.

package com.yawintutor;

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayItem {
	public static int[] removeOnce(int[] arr, int value) {
		int index = IntStream.range(0, arr.length).filter(i -> arr[i] == value).findFirst().getAsInt();
		return IntStream.range(0, arr.length).filter(i -> i!=index).map(i -> arr[i]).toArray();
	}

	public static int[] removeAll(int[] arr, int value) {
		return Arrays.stream(arr).filter(val -> val!=value).map(x->x).toArray();
	}

	public static void main(String[] args) {
		int[] arr = { 1, 1, 2, 3, 4, 3, 5 };
		int value = 3;

		System.out.print("Actual      : ");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\n\nRemove Once : ");
		arr = removeOnce(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\nRemove All  : ");
		arr = removeAll(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}	
}

Output

Actual      : 1 1 2 3 4 3 5 

Remove Once : 1 1 2 4 3 5 
Remove All  : 1 1 2 4 5 


Solution 2 – Using the same array

If the size of the array is large, you cannot duplicate the array. The element can be removed by moving the rest of the element to one index position. The code example below illustrates how to locate a specific item from the array and delete it by moving other items by an index location.

The first loop iterates the entire array and compares each element to the given value. If the value matches any of the array elements, break the index value of the loop. The second loop will iterate the array from the index and copy the element from the adjacent index. The last index is assigned to zero at the end. In this method, the array size is not reduced, instead the element is moved by an index position.

package com.yawintutor;

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayItem {
	public static void removeOnce(int[] arr, int value) {
		int i = 0;
		for( i=0; i<arr.length; i++) {
			if(arr[i]==value) break;
		}
		if(i>=arr.length) return;
		for(int j=i;j<arr.length-1;j++) {
			arr[j]=arr[j+1];
		}
		arr[arr.length-1]='\0';
	}

	public static void removeAll(int[] arr, int value) {
		int count = 0;
		for(int i=0; i<arr.length; i++) {
			if(arr[i]==value) count++;
		}
		if(count==0) return;
		for(int i=0,j=0;i<arr.length;i++) {
			if(arr[i]==value) continue;
			arr[j++]=arr[i];
		}
		for(int i=1;i<=count;i++) {
			arr[arr.length-i]='\0';
		}
	}

	public static void main(String[] args) {
		int[] arr= {1,3,2,3,4,3,5};
		int value =3;
		
		System.out.print("Actual      : ");
		for(int i=0; i<arr.length;i++) {
			System.out.print(arr[i]+" ");
		}

		System.out.print("\n\nRemove Once : ");
		removeOnce(arr,value);
		for(int i=0; i<arr.length;i++) {
			System.out.print(arr[i]+" ");
		}
		
		System.out.print("\nRemove All  : ");
		removeAll(arr,value);
		for(int i=0; i<arr.length;i++) {
			System.out.print(arr[i]+" ");
		}
	}
}

Output

Actual      : 1 1 2 3 4 3 5 

Remove Once : 1 1 2 4 3 5 
Remove All  : 1 1 2 4 5 


Solution 3 – Using two array

If the size of the array is small, the array can be duplicated. The element can be removed by coping with the rest of the items. The code example below demonstrates how to locate a specific item from the array and copy all items except the specific item.

package com.yawintutor;

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayItem {
	public static int[] removeOnce(int[] arr, int value) {
		int i = 0;
		for (i = 0; i < arr.length; i++) {
			if (arr[i] == value)
				break;
		}
		if (i >= arr.length)
			return arr;

		int[] tmpArray = new int[arr.length - 1];
		for (int j = 0; j < i; j++) {
			tmpArray[j] = arr[j];
		}
		for (int j = i; j < arr.length - 1; j++) {
			tmpArray[j] = arr[j + 1];
		}
		return tmpArray;
	}

	public static int[] removeAll(int[] arr, int value) {
		int count = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value)
				count++;
		}
		if (count == 0)
			return arr;
		int[] tmpArray = new int[arr.length - count];

		for (int i = 0, j = 0; i < arr.length; i++) {
			if (arr[i] == value)
				continue;
			tmpArray[j++] = arr[i];
		}
		return tmpArray;
	}

	public static void main(String[] args) {
		int[] arr = { 1, 3, 2, 3, 4, 3, 5 };
		int value = 3;

		System.out.print("Actual      : ");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\n\nRemove Once : ");
		arr = removeOnce(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\nRemove All  : ");
		arr = removeAll(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}

Output

Actual      : 1 1 2 3 4 3 5 

Remove Once : 1 1 2 4 3 5 
Remove All  : 1 1 2 4 5 


Solution 4 – Using System array copy

Java supports copying partial elements from an array to another array. Api System.arraycopy() can help you copy a sub array to another array. This api can be used to remove a specific item from an array by copying the array without a specific item.

package com.yawintutor;

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayItem {
	public static int[] removeOnce(int[] arr, int value) {
		int index = -1;
		for (index = 0; index < arr.length && arr[index] != value; index++) ;

		if (index >= arr.length)
			return arr;
		
		int[] tmpArray = new int[arr.length - 1];
		System.arraycopy(arr, 0, tmpArray, 0, index); 
        System.arraycopy(arr, index + 1, tmpArray, index, arr.length - index - 1); 
		return tmpArray;
	}

	public static int[] removeAll(int[] arr, int value) {
		int index = -1;
		while(true) {
			for (index = 0; index < arr.length && arr[index] != value; index++) ;
			if (index >= arr.length) break;
			int[] tmpArray = new int[arr.length - 1];
			System.arraycopy(arr, 0, tmpArray, 0, index); 
	        System.arraycopy(arr, index + 1, tmpArray, index, arr.length - index - 1);
	        arr = tmpArray;
		}
		return arr;
	}

	public static void main(String[] args) {
		int[] arr = { 1, 3, 2, 3, 4, 3, 5 };
		int value = 3;

		System.out.print("Actual      : ");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\n\nRemove Once : ");
		arr = removeOnce(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\nRemove All  : ");
		arr = removeAll(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}

Output

Actual      : 1 1 2 3 4 3 5 

Remove Once : 1 1 2 4 3 5 
Remove All  : 1 1 2 4 5 


Solution 5 – Using ArrayList

The array can be converted to an ArrayList.  The ArrayList is a java object that supports several methods for executing different operations. If the removal of a specific item is dependent on criteria, then the array list is the preferred way to delete a specific item from the array.

package com.yawintutor;

import java.util.Arrays;
import java.util.stream.IntStream;

public class ArrayItem {
	public static int[] removeOnce(int[] arr, int value) {
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0; i<arr.length; list.add(arr[i]), i++);
		list.remove(list.indexOf(value));
		int arr1[] = new int[list.size()]; 
		for(int i=0; i<list.size(); arr1[i]=list.get(i), i++);
		return arr1;
	}

	public static int[] removeAll(int[] arr, int value) {
		List<Integer> list = new ArrayList<Integer>();
		for(int i=0; i<arr.length; list.add(arr[i]), i++);
		while(list.indexOf(value)>-1) list.remove(list.indexOf(value));
		int arr1[] = new int[list.size()]; 
		for(int i=0; i<list.size(); arr1[i]=list.get(i), i++);
		return arr1;
	}

	public static void main(String[] args) {
		int[] arr = { 1, 3, 2, 3, 4, 3, 5 };
		int value = 3;

		System.out.print("Actual      : ");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\n\nRemove Once : ");
		arr = removeOnce(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}

		System.out.print("\nRemove All  : ");
		arr = removeAll(arr, value);
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}

Output

Actual      : 1 1 2 3 4 3 5 

Remove Once : 1 1 2 4 3 5 
Remove All  : 1 1 2 4 5 



Leave a Reply