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
Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

RELATED POSTS

Concatenate Strings Using StringBuilder.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these...

Getting Started With Hibernate

Introduction Hibernate is a framework which allows you to access database tables from Java code. Before Hibernate came into existence, JDBC was widely used. JDBC...

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

Â

MORE IN THIS CATEGORY

Remove Duplicates from List Using HashSet

The Set is also an interface in the 'Java Collection' framework. Unlike a List, a Set does not allow duplicates. Hence you can...

How To Customize GitBash For Windows

By default the GitBash prompt settings / configuration come from shell script called 'git-prompt.sh'. This is usually hosted inside 'profile.d' directory inside the GitBash...

Rounding Decimal Number With Java DecimalFormat

The DecimalFormat class has a method called setRoundingMode() which can be used for setting the rounding mode for DecimalFormat object. The setRoundingMode() accepts RoundingMode...

RECENT 'HOW-TO'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string with a function or variable. It does not...

SIMILAR ON CODEX

- Advertisement -spot_img