How can I transfer duplicates to another ArrayList?
I have a list of arrays
ArrayList<String> list=new ArrayList<String>();
list.add("Apple");
list.add("Ball");
list.add("Ball");
list.add("Cat");
list.add("Cat");
list.add("dog");
and I want to pass duplicate lines to another ArrayList.
I mean the 2nd array list should only contain Ball and Cat, not Apple and dog.
Any help is greatly appreciated.
source to share
Try the following:
// Custom list to ensure that one duplicate gets added to a list at most as
// opposed to n-1 instances (only two instances of a value in this list would
// be deceiving).
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Ball");
list.add("Ball");
list.add("Ball");
list.add("Ball");
list.add("Cat");
list.add("Cat");
list.add("Cat");
list.add("dog");
list.add("dog");
Set<String> set = new HashSet<>();
Set<String> setOfDuplicates = new HashSet<>();
for (String s : list) {
if (!set.add(s)) { // Remember that sets do not accept duplicates
setOfDuplicates.add(s);
}
}
List<String> listOfDuplicates = new ArrayList<>(setOfDuplicates);
source to share
You can use Set
to help identify duplicate items and then simply return ArrayList
those items.
public static ArrayList<String> retainDuplicates(ArrayList<String> inputList){
Set<String> tempSet = new HashSet<>();
ArrayList<String> duplicateList = new ArrayList<>();
for (String elem : inputList) {
if(!tempSet.add(elem)) duplicateList.add(elem);
}
return duplicateList.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
}
call the method like this:
ArrayList<String> resultList = retainDuplicates(list);
Note that I used distinct()
to remove any items that occur more than once in the duplicateList
. However, if you want to keep duplicates regardless of their occurrence in duplicateList
, just do return duplicateList;
instead return duplicateList.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
.
source to share
since you said that your duplicates will be next to each other, you can repeat this list in pairs, and if the paired items match, there is a duplicate
here would be a generic pseudocode for it
int first = 0
int second = 1
for (arraySize)
if (array[first] == array[second])
//there is a match here
newArray.add(array[first])
first += 1
second += 1
Note that this does not check for array bounds which should be easy to implement
now, as for the second list having no duplicate elements, you can just store the variable with the last passed element, and if the new duplicate found is the same, don't send it again
source to share
ArrayList<String> list=new ArrayList<String>();
list.add("Apple");
list.add("Ball");
list.add("Ball");
list.add("Cat");
list.add("Cat");
list.add("dog");
List<String> duplicateList= new ArrayList<String>();
for(String str: list) {
if(Collections.frequency(list, str) > 1) {
duplicateList.add(str);
}
}
System.out.println(duplicateList.toString());
//Here you will get duplicate String from the original list.
source to share