Site icon Automation Dojos

Remove Duplicates from List Using For-Loop

The simplest way to remove duplicates from a ‘List’ is to use a for-loop. The following code snippet demonstrates this method.

First, the code creates a new ArrayList to store the output, i.e. a List without duplicates. A for-loop is then used which iterates through the input List. It first checks if the input element is present in the output List and if not, it adds it to the List.

Finally, another for loop is used to print each element in the output list.

So this code prints the following output:

5 10 15 20 35 40 25
Exit mobile version