Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java program for finding repeating element in an array

Repeating element of an array

In this section, we will learn the Java Program to Find the Elements that have Duplicates.

Given an array, print all element whose frequency is one.

Example

Input: a[]= { 1,2,5,2,6,7,5 }

Output: 2,5

There is two number which has more than one frequency.

so there is two number which is repeated.

Java program for repeating element in an arrray

Algorithm

Step 1. Initialize arrays.

Step 2. Declare the scanner class for taking input.

Step 3. take array size from the user.

Step 4. Take an element of the array from the user.

Step 5.  Call the Repeating method which gives us repeating element from the array.

Repeating(int a[], int size)

Step 1.  Initialize the outer for loop for process each element of the array.

Step 2.  Initialize the inner loop for checking that the current number match with other number or not. if the current element match with another element.

Step 3. print the array element.

 

Java Program

import java.util.Scanner;
 class Main
{
public static void Repeating(int a[],int n)
{
     int count;
     System.out.println("Repeating number in array");
     for(int i=0;i<n;i++)
     {
        count=0;
        for (int j = i+1; j < n; j++)
        {
              if(a[i]==a[j] && i!=j)
              System.out.print(a[i]+" ");
        }
     }
}
  public static void main(String[] args)
  {
     Scanner sc=new Scanner(System.in);
     int[] a=new int[50];
     System.out.println("enter size of an array");
     int size=sc.nextInt();
     System.out.println("enter elements of an array");
     for(int i=0;i<size;i++)
     {
         a[i]=sc.nextInt();
     }
     Repeating(a,size);
 }
}
output
Enter size of an array
6
Enter elements in an array
12
2
12
3
9
2
The repeating elements are
2,12