I am trying to make a remove method that works on an array implementation of a list. Can I set the the duplicate element to null to remove it? Assuming that the list is in order.
ArrayList a = new ArrayList[];
public void removeduplicates(){
for(a[i].equals(a[i+1]){
a[i+1] = null;
}
a[i+1] = a[i];
}
No, an array element containing a
null
is still there, it just doesn't contain any useful value.You could try moving every element from further down in the list up by 1 element to fill the gap, then you have a gap at the end of the array - the array will not shrink from doing this!
If you're doing this a lot, you can use
System.arraycopy()
to do this packing operation quickly.Use ArrayList.remove(int index).
But be careful when using for-loops and removing objects in arrays.
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
The straight-forward answer to your question is that setting an array or ArrayList element to null gives you a null entry in the array or ArrayList. This is not the same thing as removing the element. If just means that
a[i]
ora.get(i)
will returnnull
rather than the original element.The code in the question is garbled. If you are going to use an ArrayList, the simplisitic solution would be something like this:
but in the worst case, that is
O(N**2)
because each call toremove
copies all elements at indexes greater than the current value ofi
.If you want to improve the performance, do something like this:
(This is a quick hack. I'm sure it could be tidied up.)
No you can't remove an element from an array, as in making it shorter. Java arrays are fixed-size. You need to use an
ArrayList
for that.If you set an element to null, the array will still have the same size, but with a null reference at that point.
Is this a homework question?
Your problem is analogous to the stream processing program
uniq
: Preserve -- by way of copying -- any element that doesn't match the one before it. It only removes all duplicates if the sequence is sorted. Otherwise, it only removes contiguous duplicates. That means you need to buffer at most one element (even if by reference) to use as a comparison predicate when deciding whether to keep an element occurring later in the sequence.The only special case is the first element. As it should never match any preceding element, you can try to initialize your buffered "previous" element to some value that's out of the domain of the sequence type, or you can special-case your iteration with a "first element" flag or explicitly copy the first element outside the iteration -- minding the case where the sequence is empty, too.
Note that I did not propose you provide this operation as a destructive in-place algorithm. That would only be appropriate with a structure like a linked list with constant-time overhead for removing an element. As others note here, removing an element from an array or vector involves shuffling down successor elements to "fill the hole", which is of time complexity n in the number of successors.
Your code example was quite confusing. With
ArrayList[]
you showed an array of ArrayList objects.Assuming that you're talking about just the
java.util.ArrayList
, then the most easy way to get rid of duplicates is to use ajava.util.Set
instead, as mentioned by others. If you really want to have, startwith, or end up with aList
for some reasons then do:The
LinkedHashSet
is chosen here because it maintains the ordering. If you don't worry about the ordering, aHashSet
is faster. But if you actually want to have it sorted, aTreeSet
may be more of value.On the other hand, if you're talking about a real array and you want to filter duplicates out of this without help of the (great) Collections framework, then you'd need to create another array and add items one by one to it while you check if the array doesn't already contain the to-be-added item. Here's a basic example (without help of
Arrays.sort()
andArrays.binarySearch()
which would have eased the task more, but then you would end up with a sorted array):Hope this gives new insights.