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!
Post your code between [code]...[/code] tags along with a stack trace.
> 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.
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!>
Yes the method orderPoints() orders the points first according to x-values and then according to y-values.
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.
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?
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.
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
> 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!
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.
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.
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.