Hi this is a sample example of java how we can write a ArrayList and eliminate duplicates in it .Below is the code snippet that gives you an idea on how to eliminate duplicates from arraylist
import java.util.ArrayList; public class ArrayListWithoutDuplicates { public static void main(String args[]){ ArrayList<String> original = new ArrayList<String>(); ArrayList<String> eliminateDuplicate= new ArrayList<String>(); original.add("Naveen"); original.add("Rajesh"); original.add("Pradeep"); original.add("Rajesh"); for (String dupWord : original) { if (!eliminateDuplicate.contains(dupWord)) { eliminateDuplicate.add(dupWord); } } System.out.println(eliminateDuplicate); } }
Output : [Naveen, Rajesh, Pradeep]
No comments:
Post a Comment