How to remove specific count of duplicates from List in java

import java.util.ArrayList;
import java.util.List;

public class IntranetExample {
 
 public static void main(String[] args) {
 
 List empList=new ArrayList<>();
 List deductList=new ArrayList<>(); //mylist after removing 3 duplicates
 List exmptList=new ArrayList<>(); // mylist with 3 duplicates

 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-335");
 empList.add("ID-336");
 empList.add("ID-336");
 empList.add("ID-336");
 empList.add("ID-336");
 empList.add("ID-336");
 
 String oldId="";
 int count=1;
 int dupCount=3;
 for (String string : empList) {
 if(oldId.equals(string)){
 if(count<dupCount+1){  
exmptList.add(string);
}
else if(count>dupCount){
 deductList.add(string);
 }
 
 }else{
 exmptList.add(string);
 count=1;
 }
 count++;
 oldId=string;
 
 }

 System.out.println("exmptList ==>> "+exmptList); 
 System.out.println("deductList ==>> "+deductList); 
 }

Output:

exmptList ==>> [ID-335, ID-335, ID-335, ID-336, ID-336, ID-336]
deductList ==>> [ID-335, ID-335, ID-335, ID-335, ID-336, ID-336]
Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s