Last active
January 8, 2017 17:09
Get Duplicate Elements https://www.v2ex.com/t/333143
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.v2ex.utils; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
public class DuplicateElementUtils{ | |
public static <T> List<T> getDuplicateElements(List<T> list){ | |
List<T> result = new ArrayList<T>(); | |
Set<T> set = new HashSet<T>(); | |
for(T e : list){ | |
if(!set.add(e)){ | |
result.add(e); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment