Solution Review: Remove Duplicates From an ArrayList
In this review, solution of the challenge 'Remove Duplicates From an ArrayList' from the previous lesson is provided.
We'll cover the following...
Solution
Press + to interact
class DuplicatesRemover {public static void removeDuplicates(ArrayList < Character > arrList) {for (int i = 0; i < arrList.size(); i++) {for (int j = i + 1; j < arrList.size(); j++) {if (arrList.get(i).equals(arrList.get(j))) { //check if there is any duplicatearrList.remove(j); //remove duplicatej--; // j is decremented}}}}public static void main( String args[] ) {ArrayList<Character> input = new ArrayList<Character>(Arrays.asList('d', 'c','a', 'b', 'b', 'c', 'c', 'c', 'd'));System.out.println("Array List before calling removeDuplicates");for (int i = 0; i < input.size(); i++){System.out.print(input.get(i)+ " ");}System.out.println();removeDuplicates(input);System.out.println("Array List after calling removeDuplicates");for (int i = 0; i < input.size(); i++){System.out.print(input.get(i)+ " ");}System.out.println();}}