Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# 2D Array removing Duplicates

dustin kab
Greenhorn
Posts: 7
I'm importing a txt file, and then filling the 2D array with the points, all that is working fine. I can't for the life of me get it working where I can find duplicate points and then basically create a new array with only single points, no duplicates.

Since they're coordinates I only want to remove a duplicate POINT, so both X and Y have to match. If only X or only Y match, the points should stay.

I'm using the processing IDE.

Paul Clapham
Sheriff
Posts: 21416
33
Your first step should be to figure out what you want to end up with. An "array"? Okay... what kind of an array? And what should be in it?

dustin kab
Greenhorn
Posts: 7
I want to come up with a 2D array of points that each POINT is an individual. So there are no duplicate points, if there is a cleaner way of simply removing those points and shrinking that Array I would like to do that.

(by POINT I mean, both [x][y] are not the same as any other [x][y])

Ultimate Goal:
- Create a 2D array that contains no duplicate points.

Campbell Ritchie
Sheriff
Posts: 50277
80
dustin kab wrote:I'm importing a txt file, and then filling the 2D array with the points, all that is working fine. I can't for the life of me get it working where I can find duplicate points and then basically create a new array with only single points, no duplicates.

Since they're coordinates I only want to remove a duplicate POINT, so both X and Y have to match. If only X or only Y match, the points should stay.

I'm using the processing IDE.

Welcome to the Ranch

Why don't you put the points into a Set (eg a Tree) as you read them from the text file. Using the return value from the add() method tells you that you have a unique value, and you can add it to your array.

[pedantic mode]There is no such thing as a 2D array in Java™, only an array of arrays.[/pedantic mode]

dustin kab
Greenhorn
Posts: 7
Hey thanks! I've never worked with sets before.

In order to get both points I figured I had to create 2 separate sets.

I got it going converting 2 single arrays into an array of arrays:

Thanks a ton, if you know how to make this more efficient I'm all ears.

Paul Clapham
Sheriff
Posts: 21416
33
I think your problem would be a lot less confusing if you just said "I have an array of points and I want to remove all of the duplicates, so I end up with an array of unique points". This business of using a two-dimensional array of floats where each row represents a point (by having exactly two columns) just adds a layer of obfuscation to your problem description.

dustin kab
Greenhorn
Posts: 7
Thanks for the tips, I'll be more clear next time. Either way, it's working now, thanks for the help.

 It is sorta covered in the JavaRanch Style Guide.