Reputation: 115
I have an Arraylist of names, I want to see duplicated values if exist and print this value. The problem is that I'm getting confused on whether to use contains method or not, the below code is not working.
ArrayList<String> list=new ArrayList();
list.add("Sagio Mane");
list.add("Karius");
list.add("Mo Salah");
list.add("Firmino");
list.add("Lovren");
list.add("Steven Gerrard");
list.add("Karius");
list.add("Mo Salah");
for(int i =0; i < list.size(); i++) {
if list.contains(list.get(i)) {
System.out.println(list.get(i)+" is duplicated")
}
}
This should print "karius is duplicated"
Upvotes: 8
Views: 40926
Reputation: 54672
If you want to reduce some time complexity at the cost of O(n)
space, you can use a Set
Set<String> set = new HashSet<>();
for(int i =0; i < list.size(); i++) {
if (set.contains(list.get(i))) {
System.out.println(list.get(i)+" is duplicated");
} else set.add(list.get(i));
}
Upvotes: 1
Reputation: 29
import java.util.ArrayList;
import java.util.HashMap;
public class CollectionsEx2 {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList();
list.add("Sagio Mane");
list.add("Karius");
list.add("Mo Salah");
list.add("Firmino");
list.add("Lovren");
list.add("Steven Gerrard");
list.add("Karius");
list.add("Mo Salah");
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(String i: list) {
if (hm.containsKey(i)) hm.put(i, hm.get(i)+1);
else hm.put(i, 1);
}
System.out.println("HashMap:" + hm);
}
}
O/p: HashMap:{Firmino=1, Sagio Mane=1, Lovren=1, Karius=2, Steven Gerrard=1, Mo Salah=2}
Upvotes: 0
Reputation: 5331
Try this working code:
ArrayList<String> list = new ArrayList<String>();
list.add("Sagio Mane");
list.add("Karius");
list.add("Mo Salah");
list.add("Firmino");
list.add("Lovren");
list.add("Steven Gerrard");
list.add("Karius");
list.add("Mo Salah");
Set<String> s = new HashSet<String>();
for(String name : list) {
if(s.add(name) == false)
System.out.println(name + "is duplicated");
}
Output:
Kariusis duplicated
Mo Salahis duplicated
Upvotes: 6
Reputation: 34
List<String> newList = new ArrayList();
Java 8:
oldList.forEach( {
if (newList.contains(e)) {
Sytem.out.println(e+" is duplicated");
} else {
newList.add(e);
}
});
Java 7: you can use customised for loop to get each from oldList.
Upvotes: 0
Reputation: 415
you can follow this Find the duplicate elements in arraylist and display
public static void main(String[] args) {
ArrayList<String> list = new ArrayList();
list.add("Sagio Mane");
list.add("Karius");
list.add("Mo Salah");
list.add("Firmino");
list.add("Lovren");
list.add("Steven Gerrard");
list.add("Karius");
list.add("Mo Salah");
HashMap<String, Integer> map = new LinkedHashMap<>(); // If you want insertion order same
list.forEach((string) -> {
if (map.containsKey(string)) {
map.put(string, map.get(string) + 1);
} else {
map.put(string, 1);
}
});
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
if (value > 1) {
System.out.println(key + " is duplicated");
}
}
}
Upvotes: 0
Reputation: 59950
If you are using Java 8+ you can use :
//Get frequencies of each element
Map<String, Long> frequencies = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
//then filter only the inputs which have frequency great than 1
frequencies.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.forEach(entry -> System.out.println(entry.getKey()));
Outputs
Karius
Mo Salah
Upvotes: 8
Reputation: 18245
You can use two sets. This is bettwer for performance O(n)
, because you have to loop an array only once, but less effective with the memory O(n)
:
public static Set<String> getDuplicates(List<String> list) {
Set<String> res = new HashSet<>();
Set<String> existed = new HashSet<>();
list.forEach(str -> {
if (!existed.add(str))
res.add(str);
});
return res;
}
Another option, is to use streams:
public static Set<String> getDuplicates(List<String> list) {
return list.stream().filter(i -> Collections.frequency(list, i) > 1).collect(Collectors.toSet());
}
Upvotes: 0
Reputation: 51892
You can use lastIndexOf to check for duplicates
for(int i =0; i < list.size(); i++) {
if (list.lastIndexOf(list.get(i)) != i) {
System.out.println(list.get(i)+" is duplicated");
}
}
Note that if you have triplicates or more this will print "xxx is duplicated" several times for the same name but you only asked to check for duplicates so this solution should be enough.
Upvotes: 3