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

Share This Post

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

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

package – task // public class- DupNum

package task;

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

public class DupNum {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("How Many Numbers You want to Enter:");
        int n = input.nextInt();

        List<Integer> number = new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            int t = input.nextInt();
            number.add(t);
        }

        
        System.out.print("Duplicates: ");
        int k=number.size();
        int l=0;

        for (int i = 0; i < k-1; i++) {
            for (int j = i + 1; j < number.size(); j++) {
                if (number.get(i)==(number.get(j)) ) {
                    l++;
                    number.remove(j);

                }
                

            }
            if(l>0)System.out.print(" "+number.get(i) );
            l=0;
        }
        System.out.println(" ");

    }
}

Programmer- Mustahid Hasan

Student of Computer Science Engineering Student
Daffodil International University
How to find duplicate number in array List in java[User Input] 1

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