Do you want to identify duplicates elements from Java List?
The method add
of set
returns a boolean whether a value already exists (true if it does not exist, false if it already exists, see Set documentation).
So just iterate through all the values.
Java Code:
package com.crunchify.tutorials; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; public class CrunchifyFindDuplicateInList { /** * @author Crunchify.com */ public static void main(String[] args) { List<String> list = new LinkedList<String>(); for (int i = 0; i < 10; i++) { list.add(String.valueOf(i)); } for (int i = 0; i < 5; i++) { list.add(String.valueOf(i)); } System.out.println("My List : " + list); System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list)); } public static Set<String> findDuplicates(List<String> listContainingDuplicates) { final Set<String> setToReturn = new HashSet<String>(); final Set<String> set1 = new HashSet<String>(); for (String yourInt : listContainingDuplicates) { if (!set1.add(yourInt)) { setToReturn.add(yourInt); } } return setToReturn; } }
Output:
My List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4] Here are the duplicate elements from list : [3, 2, 1, 0, 4]