Repeating elements of an array using java

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.

now we will discuss java program and working to find the repeating number from the array.

repeating

Working

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 to find the repeated elements in the array

import java.util.Scanner;
public 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 of an array
1
2
1
2
4
5
Repeating number in array
1  2

Please Login/Signup to comment