# i need help with my code.how can i eliminate duplicate number from my sorted list

• 04-13-2009, 03:40 AM
yinghang
i need help with my code.how can i eliminate duplicate number from my sorted list
import java.util.*;
// Class SortedDoubleUniqueList can be used to store a list of doubles.

public class SortedDoubleUniqueList {
private double[] elementData; // list of doubles
private int size; // current number of elements in the list
private boolean unique = false; //duplicates allowed when unique is false

public static final int DEFAULT_CAPACITY = 100;

// post: constructs an empty list of default capacity
public SortedDoubleUniqueList() {
this(DEFAULT_CAPACITY);
}

// pre : capacity >= 0
// post: constructs an empty list with the given capacity
public SortedDoubleUniqueList(int capacity) {
elementData = new double[capacity];
size = 0;
}

// post: returns the current number of elements in the list
public int size() {
return size;
}

// pre : 0 <= index < size()
// post: returns the double at the given index in the list
public double get(int index) {
return elementData[index];
}

// post: creates a comma-separated, bracketed version of the list
public String toString() {
if (size == 0) {
return "[]";
} else {
String result = "[" + elementData[0];
for (int i = 1; i < size; i++)
result += ", " + elementData[i];
result += "]";
return result;
}
}

//post : returns the position of the first occurence of the given
public double indexOf(double value) {
int position = Arrays.binarySearch(elementData,0,size,value);
if(position < 0){
return -1;
}
return position;
}

// pre : size() < capacity (elementData.length)
// post: appends the given value to the end of the list
if((unique && indexOf(value)== -1) || !unique ){
int positionIndex=0;
for (int i=0;i<size;i++){
if (value>=elementData[i]){
positionIndex++;
}else{
break;
}
}
for (int i=size;i>positionIndex;i--){
elementData[i]=elementData[i-1];
}
elementData[positionIndex]=value;
size++;
}
}
}

Here is test code:

public class SortedDoubleUniqueListTest {
public static void main(String[] args) {
SortedDoubleUniqueList list = new SortedDoubleUniqueList();

System.out.println("list = " + list);

}
}
• 04-13-2009, 03:48 AM
Fubarable
set unique to true?
• 04-13-2009, 04:00 AM
yinghang
can you say it more specific? where and how should i change my code?thanks.
• 04-13-2009, 04:04 AM
Fubarable
reread this line of code and the comment:
private boolean unique = false; //duplicates allowed when unique is false

that tells me (and the program logic makes sense) that if unique is set to true here, duplicates will not be allowed. Why not try it and see what happens?
• 04-13-2009, 04:07 AM
yinghang
oh my god..thanks a lot...i should recognize this earlier..
• 04-13-2009, 04:17 AM
Fubarable
lol, no problem. sometimes you must step away from a problem to see it anew.
• 04-13-2009, 04:19 AM
Fubarable
but the real meat of the code is in the add method where you encounter this:
Code:

`if((unique && indexOf(value)== -1) || !unique ){`
This states if unique is true and the value cannot be found (there is no match), then add the value. Also add the value if unique is false, regardless of whether a match is found or not.