Comparing 2D arrays: consider rows with same elements in different order being equal
in
Programming Questions
•
1 year ago
Hello everyone!
I need to compare two 2D arrays and append equal ones to a new empty array but this time I would need to two arrays to result equal even if the same elements are in different order, i.e. so {1,2,3,4} and {2,1,3,4} would be considered equal rows... Arrays.equal no longer applicable in this case.
I tried to solve this looping through each element but unfortunately this attempt failed:
I need to compare two 2D arrays and append equal ones to a new empty array but this time I would need to two arrays to result equal even if the same elements are in different order, i.e. so {1,2,3,4} and {2,1,3,4} would be considered equal rows... Arrays.equal no longer applicable in this case.
I tried to solve this looping through each element but unfortunately this attempt failed:
int [][] array1 = { //all possible combinations
{1, 2, 3, 4},
{0, 1, 2, 4},
{3, 4, 5, 6},
{2, 3, 9, 3},
{3, 4, 6, 8},
{3, 4, 12, 13}};
int [][] array2 = {//all combinations for this player
{2, 1, 3, 4},
{0, 1, 2, 4},
{3, 4, 5, 6},
{2, 3, 10, 11},
{3, 4, 12, 13}};
int [][]array3 = new int [6][4];
int count1;
for (int i=0; i<array1.length;i++) {
for (int k=0; k<array2.length;k++){
for (int j=0;j<4;j++) {
if (array1[i][j]==array2[k][j]) {
count1=0;
array3[i][j]=array1[i][j];
count1++;
}
}
}
}
Does anyone know if there is any way to solve this (probably also in a more elegant way)?
Thanks a lot in advance!
Thanks a lot in advance!
1