How to find duplicate String in array List in java[User Input]

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email
6

I am trying to write a code which will find the duplicate String in an user input arrayList. So, far I have written below code:

package – homework // public class- Assign2

package homework;
import static java.lang.reflect.Array.set;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Assign2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<String> name= new ArrayList<>();
        String s;
        int n;
        System.out.print("How many Name you want to check?-");
        n=input.nextInt();
        for(int i=0;i<n;i++)
        {
            s=input.next();
            name.add(s);
        }
        Set<String> Set=new HashSet<>();
        Set<String> duplicatename = new HashSet<>();
        for (String xx : name)
        {
            if(!Set.add(xx))
            {
                duplicatename.add(xx);
            }
        }
        
        System.out.println("Duplicate Name:"+duplicatename);
    }
}

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Do You Want To Contact With me?

drop us a line and keep in touch