Remove duplicates with linkedHashSet
private static Integer[] removeDuplicatesHash(Integer[] origArray) {
LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>( Arrays.asList(origArray) );
return linkedHashSet.toArray(new Integer[] {});
}
From Sorted Int
private static Integer[] removeDuplicatesSorted(Integer[] origArray) {
Integer[] tempArray = new Integer[origArray.length];
//Arrays.sort(origArray);
int indexJ = 0;
for (int indexI = 0; indexI < origArray.length - 1; indexI++)
{
Integer currentElement = origArray[indexI];
if (currentElement != origArray[indexI+1]) {
tempArray[indexJ++] = currentElement;
}
}
tempArray[indexJ++] = origArray[origArray.length-1];
return tempArray;
}
From UnSorted Int
private static Integer[] removeDuplicatesUnsorted(Integer[] origArray) {
for (int j = 0; j < origArray.length; j++) {
for (int i = j + 1; i < origArray.length; i++) {
if (origArray[j] == origArray[i]) {
origArray[i] = null;
continue;
}
}
}
//origArray[origArray.length - 1] = null;
return origArray;
}
With Stream
private static Integer[] removeDuplicatesStream(Integer[] origArray) {
List<Integer> listWithoutDuplicates = Arrays.asList(origArray).stream().distinct().collect(Collectors.toList());
return listWithoutDuplicates.toArray(new Integer[] {});
}