Java – Check Element is Duplicated in Array

import java.util.Scanner;
class Code
{
            public static void main(String[] args)
            {
                        Scanner scan = new Scanner(System.in);
                        int[] arr = {4, 2, 8, 1, 9, 6, 2, 7};
                       
                        System.out.print(“Enter element : “);
                        int ele = scan.nextInt();
                       
                        int count=0;
                        for (int i=0 ; i<arr.length ; i++)
                        {
                                    if(arr[i] == ele)
                                                count++;
                        }
                        if(count==0)
                                    System.out.println(“Element Not found”);
                        else if(count==1)
                                    System.out.println(“Element not duplicated”);
                        else
                                    System.out.println(“Duplicated element”);
            }
}
Scroll to Top