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

annas1019

To begin, I am doing this using solely arrays - meaning no hash maps or array lists. It is in Java.

I am trying to create a method, project, which takes in two integer arrays (of 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 line that is starred (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 would be the best way to accomplish this or correct the code?

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];**    
                
            }
        }
    }

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?

Turtle

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I find matching values in two arrays?

How do I get the intersection between two arrays as a new array?

How can I sum these two arrays into a new one?

How can I multiply two arrays with same length and return a new array with these values?

How can I count these duplicate integers?

Find if two arrays are repeated in array and then select them

Using pandas, how can I compare the values between 2 columns from two dataframes and push them to a new dataframe?

Find a duplicate in array of integers

How can I find associated array key/value in array of arrays?

How can i Shuffle two js arrays without mismatching them

How can I find the duplicated elements in a array and replace them?

How can I remove duplicate array in between two arrays

How can i replace duplicate integers in an ArrayList with the missing integers

How can I find duplicate substring text between two colums in Excel?

Find duplicate in two arrays

How can I compute the difference between array values, then average them?

Find Data Between Dates and Copy Them to New Workbook

How can I create a random array between a range of two other arrays?

How can I copy an array of arrays into clipboard using javascript

How can I find the GCD of two or more integers, javascript? No methods

how can i extract arrays from one array & convert them in object and put them in a new array?

Bug in methods that find unmatched integers between two arrays

How can I find a document by comparing the arrays of two other documents

In Reactjs, how can i pass two props with one of them is array

How can I find matches in two arrays with dictionaries in them?

how can I find the lowest value of each row and exclude them to find a new average from array

How can I loop over all directories and subdirectories and find files with a specific extension, then run FFMPEG on them, then copy them to a new lib

How can I replace integers or strings that include space between them

Compare two arrays and if there are any duplicate remove them from one array