Skip to content

Instantly share code, notes, and snippets.

@oriapp
Created October 25, 2021 21:34
Show Gist options
  • Save oriapp/b93279c31c3ce09b8935673582e05e12 to your computer and use it in GitHub Desktop.
Save oriapp/b93279c31c3ce09b8935673582e05e12 to your computer and use it in GitHub Desktop.
import java.util.*;
public class test {
static public void main(String args[]){
int[] res = Filter(new int[]{6, 9, 2, 2, 9, 4, -3}, 9);
for (int i : res){
System.out.println(i);
}
}
public static int[] Filter(int[] arr, int num){
int n = countNums(arr, num);
int[] a = new int[n];
int j = 0;
for(int i = 0; i < arr.length; i++) {
if (arr[i] != num){
a[j] = arr[i];
j++;
}
}
return a;
}
public static int countNums(int[] arr, int num){
int count = 0;
for(int i = 0; i < arr.length; i++){
if (arr[i] != num){
count++;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment