Reputation: 135
Let me start by saying this a homework question that I am having trouble with.
I have sorted an array and what I need to do is use another array to remove duplicates by iterating over the first and comparing adjacent items then adding the non duplicates to the new array. Once I have finished that I set the old array = to the new array. I am not used to java and therefore I am running into some problems I think getting the iteration setup correctly.
public static void main(String[] args) {
args = new String[] { "data/list1.txt" };
StdIn.fromFile("data/list2.txt");
// StdOut.toFile ("finished.txt");
int[] whitelist = In.readInts(args[0]);
Arrays.sort(whitelist);
int newArray[] = new int[whitelist.length];
for (int i = 0; i < whitelist.length-1; i++) {
int k = 0;
if(whitelist[i+1] > whitelist[i])
newArray[k] = whitelist[i];
k++;
StdOut.println(java.util.Arrays.toString(whitelist));
whitelist = newArray;
}
for (int i=0; i<newArray.length;i++){
StdOut.println(java.util.Arrays.toString(newArray));
}
This code piece is part of a larger binary search, but this is the portion I am having problems with.
My output besides not having the duplicates removed also prints out several times.
Any direction would be greatly appreciated.
Upvotes: 1
Views: 2557
Reputation: 1170
With the limitations like not using collections, your code can be rewritten this way, and it will work:
Arrays.sort(whitelist);
int newArray[] = new int[whitelist.length];
newArray[0] = whitelist[0];
int k = 1;
for (int i = 0; i < whitelist.length - 1; i++) {
if(whitelist[i+1] > whitelist[i]) {
newArray[k] = whitelist[i + 1];
k++;
}
}
newArray = Arrays.copyOf(newArray, k);
whitelist = newArray;
System.out.println(Arrays.toString(newArray));
Upvotes: 1
Reputation: 1170
Generally speaking you should correctly define equals() and hashCode(), to define what the word "duplicate" means for your objects. But since you use primitives and wrappers (through boxing/unboxing) you don't have to do it here.
Then you should put your array into some Set collection. All duplicates will be eliminated automatically. After that put the Set back to Array.
The Java built-in mechanism will remove duplicates in the most optimized way. You will not have to do it manually.
Integer[] whitelistI = null;
Set set = new HashSet(Arrays.asList(whitelist));
whitelistI = (Integer []) set.toArray(new Integer[set.size()]);
If you need an array of primitives you can copy it from whitelistI.
Moreover, this is wrong:
int newArray[] = new int[whitelist.length];
Your new array will be of the same length as the original one, but you said you want to delete duplicates. If you delete duplicates its actual size becomes shorter and your new array will have empty values (in your case - 0 (zeros)).
Upvotes: 0
Reputation: 69
It could help you start any work by outlining the algorithm on paper, it will help when you have to code for interviews.
Upvotes: 0
Reputation: 24780
The if
only applies to the first instruction, k++ is incremented in each iteration. You should use:
if (whitelist[i+1] > whitelist[i]) {
newArray[k] = whitelist[i];
k++;
}
Also, in the first loop you are overwritting the whilelist
with newArray
even after the first operation, I think you meant to move this outside the for
:
StdOut.println(java.util.Arrays.toString(whitelist));
whitelist = newArray;
Upvotes: 0