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

1. Member
Join Date
Apr 2009
Posts
3
Rep Power
0

## 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);

}
}

2. set unique to true?

3. Member
Join Date
Apr 2009
Posts
3
Rep Power
0
can you say it more specific? where and how should i change my code?thanks.

4. 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?

5. Member
Join Date
Apr 2009
Posts
3
Rep Power
0
oh my god..thanks a lot...i should recognize this earlier..

6. lol, no problem. sometimes you must step away from a problem to see it anew.

7. but the real meat of the code is in the add method where you encounter this:
Java 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•