Advertisement
Hiteshw11

Finding Duplicate Elements in Array

Nov 16th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import java.util.*;
  2. class Main
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Scanner src=new Scanner(System.in);
  7.         System.out.println("Enter the size of array");
  8.         int n=src.nextInt();
  9.         int arr[]=new int[n];
  10.         for(int i=0;i<n;i++)
  11.         {
  12.             System.out.println("Enter element");
  13.             arr[i]=src.nextInt();
  14.         }
  15.         System.out.println("The entered array is");
  16.         for(int j=0;j<n;j++)
  17.         {
  18.             System.out.println(arr[j]);
  19.         }
  20.         ArrayList<Integer> repeat =new ArrayList<>();
  21.         for(int k=0;k<n;k++)
  22.         {
  23.             int rp=arr[k];
  24.             for(int j=k+1;j<n;j++)
  25.             {
  26.                 if (rp==arr[j])
  27.                 {
  28.                     repeat.add(arr[j]);
  29.                 }
  30.             }
  31.         }
  32.        
  33.         System.out.println("The duplicate elements in array are "+repeat);
  34.        
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement