-3

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.

  • Possible duplicate of [How to find a duplicate element in an array of shuffled consecutive integers?](https://stackoverflow.com/questions/2605766/how-to-find-a-duplicate-element-in-an-array-of-shuffled-consecutive-integers) – Rhalp Darren Cabrera Apr 20 '18 at 16:58
  • 4
    `return IntStream.of(numbers).distinct().toArray();` – Elliott Frisch Apr 20 '18 at 16:58
  • 2
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Turing85 Apr 20 '18 at 17:00

3 Answers3

3

You wrote newNumbers[k] = num.get(k); that's not going to work because you never assigned a new integer array of the proper size to newNumbers; you assigned an array of size zero.

Another problem is this line: num.remove(numbers[i]); because you should pass the index you wish to remove, not the value itself. (Edit: actually, it's also allowed to remove(Object o), just be mindful of whether you are passing an int or an Integer there.)

Finally, there is a logical error in your removal loop. If the number appears more than two times in the input stream, then you will try to remove it too many times; to prevent this you may exit the innermost loop using a continue instruction.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
3

Steps to follow:

  1. Pay attention to the requirements
  2. Do some reading of Java Collections and streams API pages.
  3. Devise a solution that is sane.
  4. Implement the solution.

Requirements:

  1. Take an array as input.
  2. Remove duplicates.
  3. Return an array as output.

Java Collections of Interest:

  1. Set
  2. HashSet

Solution

  1. put array elements in the set.
  2. generate a new array from the set.
  3. return the new array.

Implement

Left for the student

DwB
  • 37,124
  • 11
  • 56
  • 82
0

Aside from what was already pointed out, I'd like to add the given code as a possible answer.

    public static int[] removeDuplicateInts(int[] numbers) {
    ArrayList<Integer> num = new ArrayList<Integer>();
    int current = numbers[0];
    boolean isDuplicate = false;
    for (int i = 0; i < numbers.length; i++) {
        if (current == numbers[i] && !isDuplicate) {
            isDuplicate = true;
        } else if (current != numbers[i]) {
            num.add(current);
            current = numbers[i];
            isDuplicate = false;
        }
    }
    int[] newNumber = new int[num.size()];
    for(int k = 0; k < num.size(); k ++) {
        newNumber[k] = num.get(k);
    }
    return newNumber;
}

In this the biggest difference from what you were doing, is the isDuplicate. This boolean will keep you from re-adding an int. At the end, you will have a proper sized ArrayList, with no duplicates, that you can use as the array length using the .size() method to your return Array.