We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I have a number sequence in an Arraylist:
1.589, 2.358, 6.486, 1.458, 1.458, 5.245...
What I would like to do is to remove numbers from arraylist that repeats themselves like number 1.458. I know that number repeats maximum twice. There are always 1 or more pair of objects that are the same.
But I want to remove not one of duplicated object, but all of them. I want to have sequence like this: 1.589, 2.358, 6.486, 5.245...
Is it possible to do?
Thanks.
Answers
Use FloatList instead of ArrayList<Float>:
processing.org/reference/FloatList.html
Issue sort() method to have an ordered list of values:
processing.org/reference/FloatList_sort_.html
Iterate over all values of the FloatList, but always storing last read value.
If current read value is the same as the previous stored 1, use remove() to get rid of current index:
processing.org/reference/FloatList_remove_.html
And that's all folks! >-)
Dear GoToLoop,
Thank you for a reply.
Is it possible to do similar things with vertices. That have the same location?
Thank, Petras
You mean coordinate pairs like a PVector or something? 8-X
If so, you can determine if you got any redundant 1s by their hashCode():
Actually everything is much more simple. Please look at the code below.
And if you have hemesh library. You could run in processing and see that I have two arrayLists. One is with points another also with points, but second arrayList has the same location as points in first arraylist.
What I want to do is to remove points from first arraylist that have the location as every point in second arraylist.
So sorry, I don't have all of those 3rd party libraries and my knowledge is still very limited to Processing and some Java! :o3
I've made a sketch which is able to remove duplicate PVector objects based on their hashCode().
if WB_Polygon2D & WB_Point2d & WB_Point3d classes got consistent hashCode() values based on their x & y or even z fields,
it's pretty easy to adapt my PVector example for those classes! :ar!
Take a look for yourself and see if you can figure out how it works:
A question of my own in this subject. That might be an answer also... There are interfaces that don't accept duplicates in Java, like Set, right? Would that be a case to use one of them? How to choose one?
A 2nd version that uses HashSet for removing duplicates. :)>-
It depends on the objects having consistent hashCode() based on the fields which define coordinates, like the 1st version! 8-X
That is cool and very fast.
But what I actually want to do is to delete both duplicates and original ones.
For example, if I have "vecs" arraylist of these elements:
And the final print ln output should be:
[ 1, -1, 0.0 ]
Is it possible to do so?
Strange request. It took me a while to find a solution. Not satisfied, found out another 1! :-bd
I've had to base them both on my previous example which uses Comparator + Collections.sort().
For the 2nd 1 using HashSet, although über faster, we got no control of the dup removing process! [-X
Here's my 1st attempt which** set(0, 0, 0)** all dups found.
And for only then, issue removeAll() to weed out all of the PVector objects which got itself reset:
[EDIT] Coded while GoToLoop was posting his last code...
And my 2nd version which uses regular remove() instead of death-marking them w/ set(0, 0, 0):
Nice 1, @PhiLho! By just using HashMap, it makes it JS Mode friendly! ^:)^
Anyways, I don't get why you
import
Java libraries, since HashMap is endorsed by Processing though! :-?Nonetheless, your version shorten up a bit: >:)
Thank you very much.
That is very nice, you put so much effort to help me:)
Now, I'll try to implement this script into hemesh library, but I think it would work perfectly.
Have a nice day, Petras
Good luck there, @petras_vestartas! (*)
If anything goes wrong, meaning that those "wblut" classes either don't internally implement hashCode()
or implement it using too many fields beyond the coordinate 1s, summon us up again here! =:)
Anyways, here's another PhiLho's version using HashMap<PVector, Boolean> rather than HashMap<PVector, Integer>: (~~)
I first used Map, hence the import, then I changed my mind, but forgot the import...
1 more HashMap variation using Map.Entry this time: :D
Strange. My initial code was like:
hence also my import of Map.Entry, but I got a ConcurrentModificationException, so I gave up (lack of time) and went to the reverse loop. This one works...