This is my code but it gives an out of bounds exception when I try to run it.
Given an int array, return an int array with duplicate ints removed if the array contains duplicate values.
removeDuplicateInts({3}) -> {3}
removeDuplicateInts({1, 2}) -> {1, 2}
removeDuplicateInts({7, 7}) -> {7}
removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7}
removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5})
removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5}
public static int[] removeDuplicateInts(int[] numbers) {
ArrayList<Integer> num = new ArrayList<Integer>();
int[] newNumbers = {};
for (int i = 0; i < numbers.length; i++) {
num.add(numbers[i]);
for(int j = i + 1 ; j < numbers.length; j++) {
if (numbers[i] == numbers[j]) {
num.remove(numbers[i]);
}
}
}
for(int k = 0; k < num.size(); k++){
newNumbers[k] = num.get(k);
}
return newNumbers;
}
I am not supposed to use java.util.Arrays so this makes it more challenging.