1

I am trying to get only distinct coordinates meaning coordinates (6,7) and (7,6) are the same. How can I go over an array and only get distinct coordinates? So basically I need all of the combinations of coordinates from an int[][] array.

ArrayList<Coords> coordinateList = new ArrayList<>();
int[][] coordinates = { {56,80}, {32,26}, {47,85}, {3,4}, {10,19}};


public void findDistinct(int[][] coordinates){
    for (int i = 0; i < coordinates.length; i++) {
        for (int j = 0; j < coordinates.length - 1 ; j++) {
            if (i == j) continue;
            Coords coords = new Coords(i, j);
            if(! coordinateList.contains(coords)){
                coordinateList.add(coords);
            }
        }
    }
    for(Coords coords: coordinateList){
        System.out.println(coords);
    }
}


public class Coords{

    private final x;
    private final y;

    public Coords(x,y) {
        this.y = y;
        this.x = x;
    }

    @Override
    public String toString() {
        return "Coords{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}

The output I would like to have:

Coords{x=0, x=1}
Coords{x=0, x=2}
Coords{x=0, x=3}
Coords{x=0, x=4}
Coords{x=1, x=2}
Coords{x=1, x=3}
Coords{x=1, x=4}
Coords{x=2, x=3}
Coords{x=2, x=4}
...

But at the moment I get all of the possible variations of the coordinates. How can I change the code to get the result I need?

2
  • You could override equals and hashCode of Coords and use a HashMap to keep track of coordinates that you have already seen. The equals and hashCode implementations should define some order in which x and y are considered (e.g. always consider the smaller value of x and y first). For example, hashCode could look something like { if (this.x < this.y) { return (Integer.toString(this.x) + Integer.toString(this.y)).hashCode(); } else { return (Integer.toString(this.y) + Integer.toString(this.x)).hashCode(); } } Commented Dec 3, 2018 at 18:19
  • What has the content of the coordinates array got to do with question? Commented Dec 3, 2018 at 18:38

2 Answers 2

1
for (int j = 0; j < coordinates.length; j++) 

should be

for (int j = i + 1; j < coordinates.length; j++)

That way you also no longer need to check if (i == j)

Sign up to request clarification or add additional context in comments.

4 Comments

But I seem to be missing 4 combinations when I do it like this. With the 5 coordinates that I have given here, I get only 6 combianations, but there should be 10.
Ah, you aslo need to do j < coordinates.length (or j <= coordinates.length - 1).
@Richard With this simplified logic there is no need to check the list if it contains the coordinates so the check if(! coordinateList.contains(coords)) is irrelevant
@JoakimDanielson yup, I removed all of the extra checks as the code was working fine without them
1

coordinateList.contains(coords) is going to check if the obj hashcode match or not. If you want contains method to check if coordinates of the two object matches or not, You have implement hashcode and equals method for Class Coords.

Since You want a unique list of coordinates, consider using Set data structure instead of list since contains is expense operation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.