#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Removing equal number from arrayList

edited December 2013

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.

Tagged:

## 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():

``````// forum.processing.org/two/discussion/1752/
// removing-equal-number-from-arraylist

final PVector a = new PVector(10, -5);
final PVector b = new PVector(11, -5);
final PVector c = new PVector(10, -5);

println(hex(a.hashCode()));
println(hex(b.hashCode()));
println(hex(c.hashCode()));

println();

println(a.equals(b));
println(a.equals(c));

exit();
``````
• 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.

``````import processing.core.PApplet;
import wblut.math.*;
import wblut.processing.*;
import wblut.core.*;
import wblut.hemesh.*;
import wblut.geom.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javolution.util.FastList;
import wblut.math.WB_Fast;

WB_Render render;

public void setup() {

size(1280, 720, OPENGL);
smooth();

render = new WB_Render(this);
}// setup

public void draw() {

background(255);
noStroke();
noFill();

int dim = 200;

//ArrayList of inputpolys
ArrayList<WB_Polygon2D> inputPolys = new ArrayList<WB_Polygon2D>();
ArrayList<WB_Point2d> points = new ArrayList<WB_Point2d>();

// Initialize inputPoly 1
points.add(new WB_Point2d(width / 2, height / 3 - dim));
points.add(new WB_Point2d(width / 2 + dim, height / 2 + dim / 2));
points.add(new WB_Point2d(width / 2 - dim, height / 2 + dim / 2));
inputPolys.add(new WB_Polygon2D(points));
// clear points
points.clear();
// Initialize inputPoly 2
points.add(new WB_Point2d(mouseX, mouseY - dim));
points.add(new WB_Point2d(mouseX + dim, mouseY + dim / 2));
points.add(new WB_Point2d(mouseX - dim, mouseY + dim / 2));
inputPolys.add(new WB_Polygon2D(points));
// clear points
points.clear();
// Initialize inputPoly 3
points.add(new WB_Point2d(width / 4, height / 3 - dim));
points.add(new WB_Point2d(width / 5 + dim, height / 5 + dim / 2));
points.add(new WB_Point2d(width / 5 - dim, height / 5 + dim / 2));
inputPolys.add(new WB_Polygon2D(points));
// clear points
points.clear();

noFill();
stroke(0);
strokeWeight(1);
for (WB_Polygon2D p : inputPolys) {

render.drawPolygon2D(p);
}

//end of inputpolys

stroke(0);
fill(0, 50);

List <WB_Polygon2D> intersectPolys = new ArrayList <WB_Polygon2D>();
List <WB_Polygon2D> splitPolys = new ArrayList <WB_Polygon2D>();

List<WB_Point2d> pointsInput1 = new ArrayList <WB_Point2d>();
List<WB_Point2d> pointsInput2 = new ArrayList <WB_Point2d>();
List<WB_Point2d> pointsInput3 = new ArrayList <WB_Point2d>();

List<WB_Point2d> pointsSegmentStart = new ArrayList <WB_Point2d>();
List<WB_Point2d> pointsSegmentResult = new ArrayList <WB_Point2d>();

List<WB_Line2D> splitLines = new ArrayList <WB_Line2D>();
List<WB_Line2D> splitLines1 = new ArrayList <WB_Line2D>();

List <WB_Segment2D> inputSegments = new ArrayList <WB_Segment2D>();
List <WB_Segment2D> intersectSegments = new ArrayList <WB_Segment2D>();
List <WB_IntersectionResult> axis = new ArrayList <WB_IntersectionResult>();

List<WB_Point3d> intPt = new ArrayList <WB_Point3d>();
List<WB_Point3d> intPt_ = new ArrayList <WB_Point3d>();

WB_Line2D[] sequence = new WB_Line2D;

///////////////////////////////////////////////////////////////////////////////////intersection
for (int i = 0; i < inputPolys.size(); i++) {
WB_Polygon2D a = inputPolys.get(i);
for (int j = i + 1; j < inputPolys.size(); j++) {
WB_Polygon2D b = inputPolys.get(j);
intersectPolys.addAll(WB_Polygon2D.intersection(a, b));
}//for
}//for

///////////////////////////////////////////////////////////////////////////////////getting points from intersection and input polygons
for (int i = 0; i < inputPolys.size(); i++) {
WB_Polygon2D a = inputPolys.get(i);

for (int j = 0; j < a.points.length; j++ ) {
pointsInput1.add(a.points[j]);
}//for
}//for

for (int i = 0; i < intersectPolys.size(); i++) {
WB_Polygon2D a = intersectPolys.get(i);

//delete all arguments for deep intersection
if (a.points.length > 3) {
intersectPolys.remove(i);
}

for (int j = 0; j < a.points.length; j++ ) {
pointsInput2.add(a.points[j]);
}//for
}//for
///////////////////////////////////////////////////////////////////////////////////point to bend

for (int i = 0; i < pointsInput1.size(); i++) {
pointsInput3.addAll(substractManyFromOne(pointsInput1.get(i),
pointsInput2));
}// for

///////////////////////////////////////////////////////////////////////////////////points conversion from 2D to 3D

for (int i = 0; i < pointsInput3.size(); i++) {

int coordX = (int) pointsInput3.get(i).x;
int coordY = (int) pointsInput3.get(i).y;
int coordZ = 0;

intPt.add(new WB_Point3d(coordX, coordY, coordZ));
//fill(0);
//text(i,coordX+10, coordY+10, coordZ);
}

for (int j = 0; j < intersectPolys.size();j++) {
WB_Polygon2D b = intersectPolys.get(j);
intersectSegments.addAll(b.toExplicitSegments());
}

for (int i = 0; i < intersectSegments.size(); i++) {
WB_Segment2D a = intersectSegments.get(i);
pointsSegmentStart.add(a.getPoint(0));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////here I am comparing the points I have
for (int i = 0; i < pointsSegmentStart.size(); i++) {
WB_Point2d a = pointsSegmentStart.get(i);

//for(int j = 0; j < pointsInput3.size(); j++){
if (i < pointsInput3.size()) {
WB_Point2d b = pointsInput3.get(i);
}
pointsSegmentResult.add(a);
//}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////here I am comparing the points I have

for (int i = 0; i < pointsSegmentResult.size(); i++) {
}

for (int i = 0; i < pointsSegmentResult.size(); i++) {

int coordX = (int) pointsSegmentResult.get(i).x;
int coordY = (int) pointsSegmentResult.get(i).y;
int coordZ = 0;

intPt_.add(new WB_Point3d(coordX, coordY, coordZ));
//fill(0);
//text(i,coordX+10, coordY+10, coordZ);
}

println("Intersected polys segments " +  intersectSegments.size() + " their end points " + pointsSegmentResult.size());

///////////////////////////////////////////////////////////////////////////////////points for axis

for (int i = 0; i < pointsInput3.size(); i++) {

int coordX = (int) pointsInput3.get(i).x;
int coordY = (int) pointsInput3.get(i).y;
int coordZ = 0;
intPt.add(new WB_Point3d(coordX, coordY, coordZ));

fill(0);
text(i,coordX+10, coordY+10, coordZ);

}

///////////////////////////////////////////////////////////////////////////////////rendering
for (WB_Point3d p : intPt_ ) {
fill(0, 20);
render.drawPoint(p, 10);
}
for (WB_Point3d p : intPt ) {
fill(0, 20);
render.drawPoint(p, 40);
}

stroke(0);
noStroke();
fill(200, 0, 200, 25);

for (WB_Polygon2D p : intersectPolys) {

render.drawPolygon2D(p);
//render.drawPolygon2DVertices(p, 5);
}//for
// end of intersectPolys
}// draw

List<WB_Point2d> substractManyFromOne(WB_Point2d inputPt, List<WB_Point2d> intPt) {
List<WB_Point2d> eachPtinputPt = new ArrayList<WB_Point2d>();
eachPtinputPt.add(inputPt);
List <WB_Point2d> AllFragmentsMinusOther = new ArrayList <WB_Point2d> ();
for (WB_Point2d EintPt:intPt) {//enter the intersection list
for (WB_Point2d eachPt : eachPtinputPt) {
if (EintPt.x == eachPt.x && EintPt.y == eachPt.y) {
AllFragmentsMinusOther.add(EintPt);
}//if
}//for
}//for
return AllFragmentsMinusOther;
}// ArrayList substractManyFromOne

List<WB_Point2d> substractManyFromOne_(WB_Point2d inputPt, List<WB_Point2d> intPt) {
List<WB_Point2d> eachPtinputPt = new ArrayList<WB_Point2d>();
eachPtinputPt.add(inputPt);
List <WB_Point2d> AllFragmentsMinusOther = new ArrayList <WB_Point2d> ();
for (WB_Point2d EintPt:intPt) {//enter the intersection list
for (WB_Point2d eachPt : eachPtinputPt) {
if (EintPt.x != eachPt.x && EintPt.y != eachPt.y) {
AllFragmentsMinusOther.add(EintPt);
}//if
}//for
}//for
return AllFragmentsMinusOther;
}// ArrayList substractManyFromOne
``````
• 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:

``````/**
* Remove Duplicate PVector (v1.03)
* by GoToLoop (2013/Dec)
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

import java.util.Collections;
import java.util.Comparator;

static final Comparator vecComp = new Comparator<PVector>() {
int compare(PVector v1, PVector v2) {
return Integer.signum(v1.hashCode() - v2.hashCode());
}
};

static final ArrayList<PVector> vecs = new ArrayList();

void setup() {
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );

//Unsorted w/ lotsa duplicates in it:
for (PVector p: vecs)   println(p);
println();

Collections.sort(vecs, vecComp);

// Duplicate objects are clustered together after sorting:
for (PVector p: vecs)   println(p);
println();

for ( int lastHash = 0, i = vecs.size(); i-- != 0; )
if ( lastHash == (lastHash = vecs.get(i).hashCode()) )
vecs.remove(i);

// Non-duplicate & sorted PVector objects now:
for (PVector p: vecs)   println(p);

exit();
}
``````
• 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

``````/**
* Remove Duplicate PVector II (v2.0)
* by GoToLoop (2013/Dec)
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

import java.util.HashSet;

static final ArrayList<PVector> vecs = new ArrayList();

void setup() {
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );

for (PVector p: vecs)   println(p);
println();

final HashSet noDups = new HashSet(vecs);
vecs.clear();
vecs.addAll(noDups);

for (PVector p: vecs)   println(p);

exit();
}
``````
• 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:

``````  vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1, -1) );
``````

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:

``````/**
* Remove Duplicate + Original PVector (v1.2)
* by GoToLoop (2013/Dec)
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

import java.util.Collections;
import java.util.Comparator;

static final Comparator vecComp = new Comparator<PVector>() {
int compare(PVector v1, PVector v2) {
return Integer.signum(v1.hashCode() - v2.hashCode());
}
};

static final ArrayList<PVector> vecs = new ArrayList();

static final PVector none = new PVector(Float.MIN_VALUE, Float.MAX_VALUE);

void setup() {
vecs.add( new PVector(7, 6.8) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1., -1) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(3.2, 9) );
vecs.add( new PVector(-8, 39) );

//Unsorted w/ lotsa duplicates in it:
for (PVector p: vecs)   println(p);
println();

Collections.sort(vecs, vecComp);

// Duplicate objects are clustered together after sorting:
for (PVector p: vecs)   println(p);
println();

for ( int lastHash = 0, i = vecs.size(); i-- != 0; ) {
final PVector p = vecs.get(i);

if ( lastHash == (lastHash = p.hashCode()) ) {
p.set(none);         // assigns current to an invalid value
vecs.set(i + 1, p);  // previous = current
}
}

// Duplicate objects become zeroed PVector 1s:
for (PVector p: vecs)   println(p);
println();

/*final ArrayList invalid = new ArrayList(1);
invalid.add(none);
vecs.removeAll(invalid);*/

while (vecs.remove(none));

// Non-duplicate & sorted PVector objects now:
for (PVector p: vecs)   println(p);

exit();
}
``````
• ``````import java.util.*;
import java.util.Map.Entry; // Workaround of Processing bug...

ArrayList<PVector> vecs = new ArrayList<PVector>();
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1, -1) );

println(vecs);

HashMap<PVector, Integer> frequency = new HashMap<PVector, Integer>();
for (PVector v : vecs)
{
Integer fr = frequency.get(v);
if (fr == null)
{
fr = 1;
}
else
{
fr += 1;
}
frequency.put(v, fr);
}

for (int i = vecs.size() - 1; i >= 0; i--)
{
Integer fr = frequency.get(vecs.get(i));
if (fr > 1)
{
vecs.remove(i);
}
}

println(vecs);

exit();
``````

[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):

``````/**
* Remove Duplicate + Original PVector II (v2.3)
* by GoToLoop (2013/Dec)
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

import java.util.Collections;
import java.util.Comparator;

static final Comparator vecComp = new Comparator<PVector>() {
int compare(PVector v1, PVector v2) {
return Integer.signum(v1.hashCode() - v2.hashCode());
}
};

static final ArrayList<PVector> vecs = new ArrayList();

void setup() {
vecs.add( new PVector(7, 6.8) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1., -1) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(3.2, 9) );
vecs.add( new PVector(-8, 39) );

//Unsorted w/ lotsa duplicates in it:
for (PVector p: vecs)   println(p);
println();

Collections.sort(vecs, vecComp);

// Duplicate objects are clustered together after sorting:
for (PVector p: vecs)   println(p);
println();

boolean toRemove = false;
int i = vecs.size() - 1;
PVector lastVec = vecs.get(i);

// Remove previous if it's marked for remove
// or that's the same as current:
while (i-- != 0)
if ( toRemove | (toRemove = lastVec.equals(lastVec = vecs.get(i))) )
vecs.remove(i + 1);

// Fix all-dup list case bug by removing left-over:
if (toRemove)   vecs.remove(0);

// Non-duplicate & sorted PVector objects now:
for (PVector p: vecs)   println(p);

exit();
}
``````
• edited December 2013 Answer ✓

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: >:)

``````/**
* Remove Duplicate + Original PVector III (v3.61)
* by  PhiLho (2013/Dec)
* mod GoToLoop
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

final ArrayList<PVector> vecs = new ArrayList();

vecs.add( new PVector(7, 6.8) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1., -1) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(3.2, 9) );
vecs.add( new PVector(-8, 39) );

for (PVector p: vecs)   println(p);
println();

final HashMap<PVector, Integer> freq = new HashMap();

for (PVector p: vecs) {
final Integer f = freq.get(p);
freq.put(p, f == null? 1 : f + 1);
}

int i = vecs.size();
while (i-- != 0)   if (freq.get(vecs.get(i)) > 1)   vecs.remove(i);

for (PVector p: vecs)   println(p);

exit();
``````
• 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>: (~~)

``````/**
* Remove Duplicate + Original PVector III (v3.73)
* by  PhiLho (2013/Dec)
* mod GoToLoop
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

final ArrayList<PVector> vecs = new ArrayList();

vecs.add( new PVector(7, 6.8) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1., -1) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(3.2, 9) );
vecs.add( new PVector(-8, 39) );

for (PVector p: vecs)   println(p);
println();

final HashMap<PVector, Boolean> freq = new HashMap();

for (PVector p: vecs)
freq.put(p, freq.containsKey(p)? true : false);

int i = vecs.size();
while (i-- != 0)   if (freq.get(vecs.get(i)))   vecs.remove(i);

for (PVector p: vecs)   println(p);

exit();
``````
• 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

``````/**
* Remove Duplicate + Original PVector IV (v4.11)
* by  PhiLho (2013/Dec)
* mod GoToLoop
*
* forum.processing.org/two/discussion/1752/
* removing-equal-number-from-arraylist
*/

import java.util.Map.Entry;

final ArrayList<PVector> vecs = new ArrayList();

vecs.add( new PVector(7, 6.8) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(1., -1) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(10, -5) );
vecs.add( new PVector(45, -5) );
vecs.add( new PVector(3.2, 9) );
vecs.add( new PVector(-8, 39) );

// All PVector objects within ArrayList:
for (PVector p: vecs)   println(p);
println();

final HashMap<PVector, Boolean> freq = new HashMap();

// Identify and remove duplicates:
int i = vecs.size();
while (i-- != 0) {
final PVector p = vecs.get(i);
freq.put(p, freq.containsKey(p)? vecs.remove(i) == p : false);
}

// All unique PVector objects:
for (PVector p: vecs)   println(p);
println();

// Remove original leftovers which ever had duplicates:
for (Entry<PVector, Boolean> e: freq.entrySet())
if (e.getValue())   vecs.remove(e.getKey());

// Only PVector objects which never had dups now:
for (PVector p: vecs)   println(p);

exit();
``````
• Strange. My initial code was like:

``````for (Entry<PVector, Integer> f : frequency.entrySet())
{
Integer fr = f.getValue();
if (fr > 1)
{
vecs.remove(f.getKey());
}
}
``````

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...

Sign In or Register to comment.