Search code examples
javaarrayscomputer-science

How can I find duplicate integers between two arrays and copy them into a new array?


To begin, I am doing this using only arrays (no hash maps or array lists).

I am trying to create a method, project, which inputs two integer arrays with different sizes.

I am trying to compare them in a way to get the following output: projecting [1, 3, 5, 3] to [2, 1, 6, 3, 1, 4, 5, 3] results in: [1, 3, 1, 5, 3]. However, projecting [2, 1, 6, 3, 1, 4, 5, 3] to [1, 3, 5, 3] results in: [1, 3, 5, 3].

I believe the nested for loop might be wrong as it is iterating through the first indices for the first array all the way through the second array, then going to the next.

I'm getting an ArrayIndexOutOfBoundsException for the starred line (tempArray[i] == arr1[i], I know it shouldn't be tempArray[i] but not sure what exactly to put).

I'm not sure how to compare them in a way to produce the line above, as it needs to be "in order". What could be the best way to accomplish this?

I attempted the following code:

public int[] project(int[] arr1, int[] arr2) {
    int counter = 0;
    for (int i = 0; i < arr1.length; i++) {
        for (int j = 0; j < arr2.length; j++) {
            if (arr1[i] == arr2[j]) {
                counter++;
            }
        }
    }
    
    int[] tempArray = new int[counter];
    for(int i = 0 ; i < arr1.length; i++) {
        for (int j = 0; j < arr2.length; j++) {
            if(arr1[i] == arr2[j]) {
                tempArray[i] = arr1[i]; // UNDERLINED
            }
        }
    }
}

Edit

The ArrayIndexOutOfBounds error is gone, but now my return statement is: projecting [1, 3, 5] to [2, 1, 6, 3, 1, 4, 5, 3] results in: [1, 3, 5, 0, 0], when it needs to be: projecting [1, 3, 5] to [2, 1, 6, 3, 1, 4, 5, 3] results in: [1, 3, 1, 5, 3].

Any idea what is causing this issue?


Solution

  • I think you have a typo in your first for loop

    change this to later

    arr1[i] == arr2[i]
    

    to

    arr1[i] == arr2[j]
    

    Regarding overlapping issues in the projection array, you need to maintain different value as your index rather than i or j which is used by arr1 and arr2 respectively

    int[] tempArray = new int[counter];
    int index = 0;
    
    for(int i=0 ; i < arr1.length; i++) {
        for (int j=0; j < arr2.length; j++) {
            if(arr1[i] == arr2[j]) {
                tempArray[index++] = arr1[i];
            }
        }
    }
    

    For the values which you are expecting you should loop through arr2 first

    for(int j=0 ; j < arr2.length; j++) {
        for (int i=0; i < arr1.length; i++) {
            if(arr1[i] == arr2[j]) {
                tempArray[index++] = arr1[i];
            }
        }
    }
    

    If the contents of array are not hardcoded and you dont know the order in which you want to pass, you can make use of length

      int[] ans;
      if(a.length < b.length) {
          ans = project(a, b);
      } else {
          ans = project(b, a);
      }
    

    Output:

    [1, 3, 1, 5, 3]