# Java program to find all symmetric elements in an array

## Symmetric Elements in an Array in Java

Here, in this page we will discuss the program to find all symmetric elements in an array in Java programming language. We are given with pairs and need to print those pairs which are symmetric. Here, we will discuss the following two different methods for printing symmetric pairs in the given set of pairs.

• Method 1:  Brute Force
• Method 2 : Using Hash-map

## Method 1 :

• Declare a 2D array for pairs.
• Run a outer loop from index  0 to 5
• Run an inner loop from index i+1 to 5
• Check if(arr[i]==arr[j] && arr[i]==arr[j])), then print the arr[i] and arr[i].

### Time and Space Complexity :

• Time Complexity : O(n2)
• Space Complexity : O(1)

### Method 1 : Code in Java

```class Main{

public static void main(String arg[])
{
int arr[][] = new int;

arr = 1;
arr = 2;
arr = 3;
arr = 4;
arr = 5;
arr = 1;
arr = 4;
arr = 3;
arr = 1;
arr = 5;

for (int i = 0; i < 5; i++){
for (int j = i + 1; j < 5; j++){
if (arr[i] == arr[j] && arr[i] == arr[j])
System.out.println("(" + arr[i] + ", " + arr[i] + ")");
}
}

}
}```

### Output :

`(3, 4)(5, 1)`

## Method 2 :

In this method we will use hash-map,

• The first element of pair is used as key and the second element is used as the value.
• Traverse all pairs one by one.
• For every pair, we check if its second element is in the hash table.
• If it is, then compare the first element with the value of the matched entry of the hash table.
• If the value and the first element match, then we found symmetric pairs.
• Else, insert the first element as a key and second element as value.

### Method 2 : Code in Java

```import java.util.HashMap;
class Main{

public static void main(String arg[])
{
int arr[][] = new int;

arr = 1;
arr = 2;
arr = 3;
arr = 4;
arr = 5;
arr = 1;
arr = 4;
arr = 3;
arr = 1;
arr = 5;

HashMap hM = new HashMap();

// Traverse through the given array
for (int i = 0; i < arr.length; i++)
{
// First and second elements of current pair
int first = arr[i];
int sec   = arr[i];

// Look for second element of this pair in hash
Integer val = hM.get(sec);

if (val != null && val == first)
System.out.println("(" + sec + ", " + first + ")");

else  // Else put sec element of this pair in hash
hM.put(first, sec);
}

}
}```

### Output :

`(3, 4)(5, 1)`

### 3 comments on “Java program to find all symmetric elements in an array”

• Anurag

public class symmetricPairs{

public static void main(String[] args)
{
int[][] arr = { {11, 20},{30, 40},{5, 10},{40, 30},{10, 5},{20, 5} };

for (int i = 0; i < arr.length; ++i)
{
for (int j = i + 1; j “);
System.out.print(“{” + arr[j] + “,” + arr[j] + “}”);
System.out.println();
}
}
}
}

} 0
• Anoopkumar

public class Demo{
public static void main(String args[]){
int[][] a={{11, 20}, {30, 40}, {5, 10}, {40, 30}, {10, 5}} ;
for(int i=0;i<5;i++){
for(int k=i+1;k<5;k++){
for(int j=0;j<2;j++){
if(a[i]==a[k][j]){
if(a[i]==a[k]){
System.out.println("symetic"+"(" +a[i]+","+a[i]+")");
}
}
}
}
}
}
} 0
• Ajay

#include
using namespace std;
int main()
{
int a,i,j,b;
int n;
cout<>n;
cout<<" enter elements \n";
for(i=0;i<n;i++)
{
for(j=0;j>a[i][j];
}
}
int k=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j] && a[i]==a[j])
{
b[k]=a[i];
b[k]=a[i];
k++;
}
}

}
cout<<" symmetric pairs are \n";
for(i=0;i<k;i++)
{
for(j=0;j<2;j++)
{
if(j==0)
{
cout<<"(";
}
cout<<b[i][j];
if(j==0)
{
cout<<",";
}
if(j==1)
{
cout<<")"<<" ";
}
}
}
return 0;
} 5