-2

I have to find duplicate values from an given array. I also have to print the values which are not duplicate, I could not do this part because it gives me an ArrayIndexOutOfBoundException. I caught the exception. Is any performance issue I will face for improper closing or memory leak? My code is below,

import org.apache.commons.lang.ArrayUtils;
public class Duplicates {

    public static void main(String[] args) {
        Duplicates.duplicate();
    }

    public static void duplicate() {
        // int array[]={1,22,32,66,56,754,22,0,66,55,83,21,67,45,99,666,85,37};
        int[] array = { 2, 4, 2, 5, 4, 7, 9, 2, 3, 3 };
        int tempArray[] = array;

        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 1; j < array.length;) {

                try {

                    //this part will print the duplicate values
                    if ((array[i] == (array[j])) && (i != j) && (i < j)) {
                        System.out.println("Duplicates values are: " + array[i]);

                        /* this forloop below is for removing duplicate number from
                           the entire array and gives a fresh array without              
                           that particular duplicate value */

                        int removeVal = array[i];
                        for (int k = i; k < array.length; k++) {
                            tempArray = ArrayUtils.removeElement(tempArray, removeVal);
                        }

                        j = 0;
                    } 
                      /* This part will print values not duplicate, but need to   
                      print like { 5,7,9 } together */
                    else {
                        if (i < j) {
                            j++;
                        } else {
                            j = i + 1;
                        }
                    }
                    array = tempArray;
                }

                catch (ArrayIndexOutOfBoundsException ae) {

                    ae.getMessage();
                }
            }
        }
    }
}

So what wrong I am doing here that can't print all together and gives ArrayIndexOutOfBoundException? But with the another array(the big one) commented out there it is not giving ArrayIndexOutOfBoundException.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • 1
    Wait, is your question about memory leaks or about the exception? This is totally confusing. – Drew Kennedy Jan 08 '16 at 14:02
  • The exception message and stack trace will tell you exactly where the error occurred, and what the out-of-bounds index was. This should give you a big clue as to what is going wrong. If not, then it might give *us* a clue, if only you presented it. – John Bollinger Jan 08 '16 at 14:04
  • Why are you iterating with `k`? – Erick G. Hagstrom Jan 08 '16 at 14:15
  • @ErickG.Hagstrom When I entered in this if loop part, I am already sure that it is a duplicate number. So I took that duplicate number and iterate with k to check if this same duplicate number present in any other position of the array or not. If present ;then eliminate from the array . So in the new array this duplicate value will never be present. But other duplicate values.will be there. I try to shorten code every time and think of performance. – Sharmistha Sakar Jan 08 '16 at 14:33
  • Ok, but you're using `removeElement`, which also iterates over (and copies) the entire array. So you may end up not removing each duplicate, but most of the time you'll be executing `removeElement` a lot more times than are necessary. – Erick G. Hagstrom Jan 08 '16 at 14:38
  • This does not make any sense: if (i < j) { j++; }. If j is great, make it even more greater? This is your infinity loop. – tak3shi Jan 08 '16 at 14:38
  • No, the `for` limits will terminate it. – Erick G. Hagstrom Jan 08 '16 at 14:40
  • @ErickG.Hagstrom removeElement() method in ArrayUtils removes a particular value from specified index(position) from the entire array. So it goes to the position mentioned and removes the number, It does not search the entire array. That's why I had to write the forloop with k . – Sharmistha Sakar Jan 08 '16 at 14:48
  • @tak3shi That infinitive loop I think I resolved, see my other post below. This if (i < j) { j++; } sometimes I saw, i and j values will be same, so it means in an array both forloop (i and j forloop) will compare the same number. doesn't make any sense, right. So I gave that condition , "i " should be less and "j" should be more to start comparison . – Sharmistha Sakar Jan 08 '16 at 14:53
  • Your problem is that int tempArray[] = array; does not create a copy of the Array. tempArray and array share the same object instance. See http://stackoverflow.com/questions/5785745/make-copy-of-array-java how to copy Arrays. – tak3shi Jan 08 '16 at 14:56
  • `removeElement` doesn't take an index. It searches the array from the start and removes the first instance of the element. https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#removeElement%28int[],%20int%29 – Erick G. Hagstrom Jan 08 '16 at 15:00
  • Worse, it makes a new copy of the array on each invocation. You really ought to find all instances of the duplicate element yourself, then make a single invocation of `removeAll()`. – Erick G. Hagstrom Jan 08 '16 at 15:17
  • @ErickG.Hagstrom Ok even it removes the first instance , it doesn't check the other numbers in the array, it gives a new array except the first occurrence. I have to keep that inside a for loop to check if the same number found in any other position of the array or not. Once this specific number is removed from the array, we can search the other duplicates. It will make less time for code execution. – Sharmistha Sakar Jan 08 '16 at 15:17
  • This program is to find duplicate numbers from an array. So I tried to find the shortest algorithm , Step 1: find the duplicate number, Step 2: If found, eliminate that duplicate number from the entire array, Step 3: From new array, find other duplicate number .and eliminate.Step 4: print those duplicate numbers. – Sharmistha Sakar Jan 08 '16 at 15:17
  • @ErickG.Hagstrom removeAll() would be fine to remove all elements at the same time, no need forloop. But it's not working for my eclipse. I configured org.apache.commons.lang3.ArrayUtils jar; but in my eclipse removeAll() is not listing. Have you tried removeAll()? – Sharmistha Sakar Jan 08 '16 at 15:39

2 Answers2

1

This for loop:

 for (int j = 1; j < array.length;) {

Cause an infinity loop try to replace it with:

  for (int j = 1; j < array.length;j++) {
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1

I think improper try...catch block was the cause. Now program is getting terminated . Here the code,

 import org.apache.commons.lang.ArrayUtils;
 public class Duplicates {

public static void main(String[] args) {
    Duplicates.duplicate();
}

public static void duplicate() {
    // int array[]={1,22,32,66,56,754,22,0,66,55,83,21,67,45,99,666,85,37};
    int[] array = { 2, 4, 2, 5, 4, 7, 9, 2, 3, 3 };
    int tempArray[] = array;
    try{
    for (int i = 0; i < array.length - 1; i++) {
        for (int j = 1; j < array.length;) {

                   //this part will print the duplicate values
                if ((array[i] == (array[j])) && (i != j) && (i < j)) {
                   System.out.println("Duplicates values are: " + array[i]);

                 /* this forloop below is for removing duplicate number from
                       the entire array and gives a fresh array without              
                       that particular duplicate value */

                    int removeVal = array[i];
                    for (int k = i; k < array.length; k++) {
               tempArray = ArrayUtils.removeElement(tempArray,  removeVal);
                    }

                    j = 0;
                } 
                  /* This part will print values not duplicate, but need to   
                  print like { 5,7,9 } together */
                else {
                    if (i < j) {
                        j++;
                    } else {
                        j = i + 1;
                    }
                }
               array = tempArray;

            }
            System.out.println("values are not duplicate are" + array[i]);
        }

        } catch (ArrayIndexOutOfBoundsException ae) {

        ae.getMessage();
       }

       }

       }