0

I am trying to remove duplicate element from int[] array in java. After declaring a tempArray at the result I am getting

java.lang.ArrayIndexOutOfBoundsException.

public static void main(String[] args) {
    int[] givenArray = {1,1,2,2,3,3,4,4,5,5,6,4,4,3,3,2,2,7,7,6,5,4,3,8,9,6};
    int n = givenArray.length;

    Arrays.sort( givenArray );

    int j = 0;
    int[] tempArray = {};
    for (int i = 0; i < n-1; i++) {
        if (givenArray[i] != givenArray[i+1]) {
           tempArray[j] = givenArray[i];
           j++;
        }
    }

    tempArray[j] = givenArray[n-1];

    for (int i : tempArray) {
        System.out.print(i+" ");
    }
}
vincrichaud
  • 2,218
  • 17
  • 34
Muhammad
  • 52
  • 7
  • 1
    you should add some code lines, cause otherwise its imposible to say anything esse than: you are trying to use a position of the array that does not exist – Oldskultxo Nov 30 '18 at 10:57
  • 3
    How are you expecting to put something in an array with 0 length? `int[] tempArray = {};` – jbx Nov 30 '18 at 11:00
  • You could instead use Set to achieve this. As you are just printing and there is no compulsion to return the result as an array, you can just print all the values present in the set – nbirla Nov 30 '18 at 11:01
  • if I put int[] tempArray = new int[n]; Then the result came 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; Remaining index are getting 0. – Muhammad Nov 30 '18 at 11:02
  • @Muhammad well obviously you need to keep track of how many distinct items you actually put. – jbx Nov 30 '18 at 11:03
  • Yes, an array has a fixed size, so if you remove the duplicates from an array, it has more elements than the unique ones you have left. – arcy Nov 30 '18 at 11:04
  • Thanks you all. :) I have got one answer from the link jbx shared. – Muhammad Nov 30 '18 at 11:08

2 Answers2

1

Your logic is pretty much alright Muhammad. Just 2 changes need to be made. I have listed them below:

  1. As Würgspaß has pointed out, you need to declare and instantiate the array temparray[] like this: int[] tempArray = new int[n]; With the way you have defined, it creates an empty array with size 0. And as a characteristic of an array, an array's size cannot be changed. Hence it gives you an OutOfBounds exception when you try to insert into it.

  2. Access the tempArray[] in the for loop using the usual iterative way, with the i value ranging from 0 to j(j inclusive). ==>

    for (int i = 0; i <= j; i++)
    {
        System.out.print(tempArray[i] + " ");
    }
    

Here is the overall updated code:

package removeduplicatesfromarray;

import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    int[] givenArray = {1,1,2,2,3,3,4,4,5,5,6,4,4,3,3,2,2,7,7,6,5,4,3,8,9,6};
    int n = givenArray.length;

    Arrays.sort( givenArray );

    int j = 0;
    int[] tempArray = new int[givenArray.length];
    for (int i = 0; i < n-1; i++)
    {
        if (givenArray[i] != givenArray[i+1])
        {
           tempArray[j] = givenArray[i];
           j++;
        }
    }
    tempArray[j] = givenArray[n-1];

    for (int i = 0; i <= j; i++)
    {
        System.out.print(tempArray[i] + " ");
    }

  }
}

OUTPUT:

1 2 3 4 5 6 7 8 9

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
-1

int[] tempArray = {}; defines an empty array with 0 length. Use int[] tempArray = new int[n]; to avoid ArrayIndexOutOfBoundsException.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41