Given an array like the one below, I was wondering if there is an easy way to turn this array into an array with unique values only?
This is given:
Here are 2 ideas:
Add all items to a Set, or create one with the constructor that has an array as a parameter (HashSet or TreeSet, depending on what time complexity you want). Then, for each element in the set, remove it, adding it to the next open position of a new array that is the size of the set.
Sort the array. Add the object at index 0 to an ArrayList. Start at index 1 and go to index length - 1. If the current element is not equal to the element at the previous index, add it to the ArrayList. Change the ArrayList into an array, if necessary.