Erasing equal elements in an array

I have an array of any size. It contains elements that may or may not be equal. I want a method to check for equal elements and if they occur, erase all but one of them (thus decreasing the size of the array). I am a bit stuck here so any help would be appreciated.

I have thought like this:

1. Go through the array and distinctElements++ if two successive elements are not equal

2. Create a new array, temp[distinctElements], say

3. Go through the array again and if two successive elements are not equal, put them in the array

This method may work, but I get ArrayIndexOutOfBoundException when I test it and I have really tried to debug it.

Thanks!

[694 byte] By [Zappa_Javaa] at [2007-11-27 1:23:48]
# 1
Post your code between [code]...[/code] tags along with a stack trace.
CaptainMorgan08a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 2
> 1. Go through the array and distinctElements++ if two successive elements are not> equalThis assumes that equal elements will be next to each other of course.
pbrockway2a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 3

Ok. The code is just a part of a quite big class, but I hope you get the relevant points. It is in fact a class to deal with regular polygons. The array consists of 2-dimensional points. I need to single out and erase equal points in the array.

public void eraseEqlPoints() {

int distinctPoints = 0;

this.orderPoints();

for(int i=0;i<this.getNbrOfCorners()-1;i++) {

if(!this.corners[i].equals(this.corners[i+1])) {

distinctPoints++;

}

}

if(!this.corners[this.getNbrOfCorners()-1].equals(this.corners[0])) {

distinctPoints++;

}

Point2D[] temp = new Point2D[distinctPoints];

for(int i=0;i<this.getNbrOfCorners()-1;i++) {

if(!this.corners[i].equals(this.corners[i+1])) {

temp[i]=this.corners[i];

}

}

if(!this.corners[this.getNbrOfCorners()-1].equals(this.corners[0])) {

temp[distinctPoints-1]=this.corners[this.getNbrOfCorners()-1];

}

this.corners=temp;

}

Thanks you for help!>

Zappa_Javaa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 4
Yes the method orderPoints() orders the points first according to x-values and then according to y-values.
Zappa_Javaa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 5

import java.util.*;

public class RemoveExample {

public static void removeDuplications(Object[] source) {

Set s = new HashSet(Arrays.asList(source));

s.toArray(source);

Arrays.fill(source, s.size(), source.length, null); //null out extra slots

}

public static void main(String[] args) {

String[] data = {"hello", "world", "hello"};

removeDuplications(data);

System.out.println(Arrays.toString(data));

}

}

A better solution would ne to work with Sets from the start.

DrLaszloJamfa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 6
I'm sure it would, but this seems to be far beyond my current knowledge and what I've been taught in the course (a university programming course). Is there no other way, then?
Zappa_Javaa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 7
Circular lists of things are b@stards to count. Suppose all your points were the same. How many distinct points would there be? What value does your code give distinctPoints?[Edit] In any event you should use System.out.println() to print it out to assist with debugging.
pbrockway2a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 8

Well, if all points are equal, none of the if-tests would be true and so distinctPoints would keep its value of 0 - I think!

The method actually works for arrays of only equal points. It also works for arrays of no equal points. But for all other cases it gives ArrayIndexOutOfBound... :S

Zappa_Javaa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 9
> want a method to check for equal elements and if they occur, erase all but one of> themSo the number of distinct points will be one!
pbrockway2a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 10
CaptainMorgan also suggested a stack trace. This will say where and what is going wrong. Make sure you indicate which line of your method corresponds to the one the stack trace (compiler message) refers to by number.
pbrockway2a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 11
Thanks for the help. I've added System.out.println() to the code and something is indeed wrong. I'll get back to you asap.
Zappa_Javaa at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...
# 12
Once you've got an accurate count of the distinct points, think about the counter variable i in the second pair of loops. At the moment you have it incrementing come what may, which means that the temp array will eventually run out of room.
pbrockway2a at 2007-7-12 0:13:58 > top of Java-index,Java Essentials,New To Java...