# Java program for finding repeating element in an array

## Repeating element of an array in Java

In this section, we will learn the Program to Find Repeating element of an array in java.Given an array, print all element whose frequency is one.

#### Example

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

There are two numbers which has more than one frequency,so there are two numbers which are repeated.  ## 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.

In the following program we will be using nested for loops for checking the repeating frequency of elements of the array, we’ll use a counter variable to keep a note of the frequencies, every time the counter goes above 1, we’ll know that these are the elements that are repeating itself, and hence we’ll have a count for the repeating elements in the array. Below is a Java Program for Finding the repeating elements in an array

## Java Program for Finding the Repeating Elements in an array

```import java.util.Scanner;
class Main
{
public static void Repeating(int a[],int n)
{
int count;
System.out.println("Repeating number in array");
// If the element is repeating then print the element     for(int i=0;i<n;i++)
{
count=0;
for (int j = i+1; j < n; j++)
{
if(a[i]==a[j] && i!=j) //Checking the elements
System.out.print(a[i]+" "); //printing the repeating elements
}
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] a=new int;     // Taking size of an array as a input from the user
System.out.println("enter size of an array");
int size=sc.nextInt();     // Enter the elements of the array
System.out.println("enter elements of an array");
for(int i=0;i<size;i++)
{         System.out.println("enter element of an array at index "+i+" ");
a[i]=sc.nextInt();
}
Repeating(a,size);
}
}```

## Output

```Enter size of an array
5
Enter elements of an array
enter element of an array at index 0
1
enter element of an array at index 1
2
enter element of an array at index 2
3
enter element of an array at index 3
3
enter element of an array at index 4
1
Repeating number in array
1 3

```
```Enter size of an array
10
Enter elements of an array
enter element of an array at index 0
12
enter element of an array at index 1
11
enter element of an array at index 2
3
enter element of an array at index 3
3
enter element of an array at index 4
34
enter element of an array at index 5
4
enter element of an array at index 6
5
enter element of an array at index 7
66
enter element of an array at index 8
12
enter element of an array at index 9
11
Repeating number in array
12 11 3```

### One comment on “Java program for finding repeating element in an array”

• Om Prakash

Code in Java :
import java.util.Scanner;
public class Print_repeating_elements
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter length: “);
int length = sc.nextInt();
int arr[] = new int[length];
for(int i=0; i<length; i++)
arr[i] = sc.nextInt();
sc.close();
for(int i=0; i<length; i++)
{
for(int j=0; j arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int i=0;
System.out.println(“Repeating elements are:”);
while(i<length)
{
int count = 0;
for(int j=i; j 1)
System.out.println(arr[i]);
i = i+count;
}
}
} 1