Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

ArrayList

Does anyone know how i could remove objects from an array list that are the same
ex:
1
2
4
4
4
5
changing array list values to
1
2
4
5
Thanx for any help
[211 byte] By [stormswimmer] at [2007-11-11 8:03:36]
# 1 Re: ArrayList
Hi,

ArrayList<Integer> duplicates = new ArrayList<Integer>()
Integer previous = new Integer(Integer.MIN_VALUE); // so it wont conflict with list values
for(int i=0; i<arrayList.size(); i++) // arrayList is your list
{
// first find duplicates
if(arrayList.get(i).equals(previous))
duplicates.add(i); // add the index of the duplicate

else{
previous = arrayList.get(i);
}
}

// now remove duplicates
for(int j= duplicates.size()-1; j>=0; j--)
{
arrayList.remove(duplicates.get(j));
}

Kind regards,
Noel
noelob at 2007-11-11 22:36:39 >
# 2 Re: ArrayList
If you were wanting to create a collection of discrete values while not getting rid of your original list, you can create an instance of CopyOnWriteArraySet using the CopyOnWriteArraySet( ArrayList yourArrayList) constructor - then iterate through the list. Or copy the elements to a collection implementing the Set interface yourself ...
nspils at 2007-11-11 22:37:33 >
# 3 Re: ArrayList
That method isn't working
I am trying to calculate the mode I am using 2 methods

public static ArrayList duplicates(ArrayList modual)
{
Collections.sort(modual);
Double previous = (Double)modual.get(1);
ArrayList duplicate = new ArrayList();
for(int i = 0; i < modual.size()-1; i++)
{
double temp = (Double) (modual.get(i));
if(temp == previous)
{
duplicate.add(temp);
}
else
{
previous = (Double) modual.get(i);
}
}
for(int j = 0; j <= duplicate.size()-1; j++)
{
modual.remove(duplicate.get(j));
}
return modual;
}

the purpose of this code is to take in the arraylist and remove the duplicates

public static ArrayList mode(double array[]) throws IOException
{
double n;
int freq = 0;
int Mfreq = 0;
double Mn = array[0];
ArrayList modes = new ArrayList();
PrintStream fout = new PrintStream( new FileOutputStream("output.txt"));
for (int i = 0; i < array.length; i++)
{
n = array[i];
if(i == 0 || array[i] != array[i - 1])
{
for (int j = 0; j < array.length; j++)
{
if (array[j] == n)
{
freq++;
}
}
}
if(freq != 0)
{
modes.add(freq+" "+array[i]);
}
freq = 0;
if (freq > Mfreq)
{
Mn = n;
}
}
double mode = 0.0;
double oc = 0.0;
String str [] = new String [modes.size ()];
modes.toArray (str);
ArrayList modual = new ArrayList();
for(int i = 0; i <= modes.size() -1; i ++)
{

String temp = (String) (modes.get(i));
int pos = str[i].indexOf(' ') ;
String occ = str[i].substring(0 , pos);
int occurances = Integer.parseInt(occ);
occ = str[i].substring(pos, str[i].length());
double number = Double.parseDouble(occ);
if(occurances >= oc)
{
modual.add(number);
oc = occurances;
}
}
return modual;
}

the purpose of this method is to take in an array and caluclate the mode
stormswimmer at 2007-11-11 22:38:37 >
# 4 Re: ArrayList
Does anyone know how i could remove objects from an array list that are the same
ex:
1
2
4
4
4
5
changing array list values to
1
2
4
5
Thanx for any help
The indexOf and lastIndexOf methods will work nicely.

public ArrayList removeDuplicates(ArrayList al) {
for (int i = 0; i < al.size(); i++) {
while (i != al.lastIndexOf(al.get(i))) {
al.remove(al.lastIndexOf(i));
}
}
return al;
}
That might be a bit sloppy to read... but basically, it loops through the ArrayList. At each index, it checks if that index equals the last index with the same object. If they are the same, there's only one in there. If they do not equal, it will remove all of the duplicates.
destin at 2007-11-11 22:39:42 >
# 5 Re: ArrayList
This method doesn't work either i get a ArrayIndexOutOfBoundsException
stormswimmer at 2007-11-11 22:40:36 >
# 6 Re: ArrayList
public TreeSet<Integer> removeDuplicates (ArrayList<Integer> myArray)
{
TreeSet<Integer> mySet = new TreeSet<Integer>();
mySet.addAll( myArray );
return mySet;
}

public void printSet( TreeSet<Integer> printSet )
{
for ( Integer i : printSet )
{
System.out.print( i );
}
System.out.println();
}
nspils at 2007-11-11 22:41:46 >
# 7 Re: ArrayList
How does this method work?
stormswimmer at 2007-11-11 22:42:44 >
# 8 Re: ArrayList
This method doesn't work either i get a ArrayIndexOutOfBoundsException
That's not because of my method. I just compiled and tested it; it works fine. Besides, nothing in the ArrayList class throws an ArrayIndexOutOfBoundsException; it does throw IndexOutOfBoundsExceptions though.
destin at 2007-11-11 22:43:41 >