Roedy Green said:
If your objects have an equals and hash method defined in the same way
you define dups, then you could enter them into a HashMap and detect
dups that way.
You could sort a copy of the array and put the dups into a HashMap.
Then you go through the original array looking up elts it the dup
HashMap. You delete all but the first one. This means you need
special hashed objects where you can maintain the first boolean.
This uses more ram than the first, is slower, and more complicated to
code and still needs equals and hash to match your dup definition.
A simple way is to create an array of Integer and
a Comparator singleton that extracts the intValue()
from each operand and indexes into the corresponding
array of objects to determine the relative sort.
Then walk through the "sorted" Integer[] to delete
the duplicates (by adding the non-dups to a new
array).
public class MyDataComparator
implements java.util.Comparator
{
public int compare(final Object obj1, final Object obj2)
{
final Integer ndx1;
final Integer ndx2;
final int x1;
final int x2;
final MyData md1;
final MyData md2;
final int result;
ndx1 = (Integer) obj1;
ndx2 = (Integer) obj2;
x1 = ndx1.intValue();
x2 = ndx2.intValue();
md1 = myDataArray[x1];
md2 = myDataArray[x2];
result = md1.compareTo(md2);
return result;
}
}
This assumes that your data object, MyData, implements
the Comparable interface. You would use the
Arrays.sort(Object[],java.util.Comparator) method to "sort"
the Integer array. Then walk through the "sorted" array
to get the indeces for the sorted elements in the original
array (myDataArray[]).
I would probably use an ArrayList initialized to the size of
the original array to accumulate the non-duplicate elements,
then use its toArray() method to build the new array.
I'll leave the rest as an exercise for the reader.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!