Question
Print duplicate elements of an array.
Input:
ENTER THE SIZE OF ARRAY:10
ENTER THE ELEMENT IN THE ARRAY:
1 2 3 4 5 6 7 8 9 10
ARRAY AFTER DELETING EVEN POSITION ELEMENTS:
2 4 6 8 10
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class duplicateNumbersInArray
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;
int n=0,i=0,j=0,count=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number of elements in the array");
n = sc.nextInt();
arr=new int[n];
System.out.println("Enter numbers in array");
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Duplicate numbers in array are:");
/*finding the frequency of elements the array and printing the number and frequency of it */
for ( i = 0; i < n; i++)
{
count=0;
if(arr[i]!=-9999999)
{
for(j=0;j< n;j++)
{
if(arr[j]==arr[i])
{
/* replacing the duplicate element so that it is not repeated again */
arr[j]=-9999999;
count++;
}
}
if(count>1)
{
System.out.println(i+" ");
}
}
}
}
}