There are various algorithms to do so and if you want to implement any of those you are free to do so, but if you are using java this can be done in two lines!
As you know Sets don't allow duplicate elements, so why not use this property. Convert your ArrayList (or Vector or any other Collection for that matter) to Set and convert it back to your Collection.
In below code snippet, I have an ArrayList of integers named 'listOfNumbers'.
HashSet uniqueNumbers = new HashSet(listOfNumbers);
Using HashSet will mess up the order of your elements, so if you are concerned about the order, use LinkedHashSet.
After conversion to set the duplicates are removed, convert it back to Collection object.
As you know Sets don't allow duplicate elements, so why not use this property. Convert your ArrayList (or Vector or any other Collection for that matter) to Set and convert it back to your Collection.
In below code snippet, I have an ArrayList of integers named 'listOfNumbers'.
HashSet
Using HashSet will mess up the order of your elements, so if you are concerned about the order, use LinkedHashSet.
After conversion to set the duplicates are removed, convert it back to Collection object.
ArrayList listOfUniqueNumbers = new ArrayList(uniqueNumbers);
And you are done! How cool is that :)
Comments