How to remove all occurrences of specific value from an array in Java

1 Answer

0 votes
import java.util.Arrays;

public class MyClass {
    
    public static int[] remove(int[] arr, int key) {
		return Arrays.stream(arr)
					.filter(val -> val != key)
					.toArray();
	}
    public static void main(String args[]) { 
        int[] arr = { 3, 2, 1, 3, 3, 7, 5, 4, 3, 3, 3, 3 };		
        int key = 3;		
        
        arr = remove(arr, key);		
        
        System.out.println(Arrays.toString(arr));
    }
}
   
   
   
/*
run:
   
[2, 1, 7, 5, 4]
  
*/

 



answered Apr 19, 2020 by avibootz
...