# C++ program to find all symmetric elements in an array

## Symmetric Elements in an Array in C++

Here, in this page we will discuss the program to find all symmetric elements in an array in C++ programming language. We are given with pairs and need to print those pairs which are symmetric. ## Method Discussed :

• Method 1 : Using Naive Approach
• Method 2 : Using Hash-Map

Let’s Discuss both methods one by one,

## 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 C++

Run
```#include<bits/stdc++.h>
using namespace std;

int main ()
{
int arr;
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])
cout << "(" << arr[i] << ", " << arr[i] << ") ";
}
}

return 0;

}
```

### Output

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

## Method 2 :

In this method we will use the map to find the symmetric pairs.

• Declare an unordered_map<int, int>mp.
• Run a loop from 0 to 5,
• Declare two variables first = arr[i], second = arr[i].
• Check if(mp.find(sec) != mp.end() and mp[sec] == first ), then print sec and first.

### Time and Space Complexity :

• Time Complexity : O(n)
• Space Complexity : O(n)

### Method 2 : Code in C++

Run
```#include<bits/stdc++.h>
using namespace std;

int main ()
{
int arr;
arr = 1;
arr = 2;
arr = 3;
arr = 4;
arr = 5;
arr = 1;
arr = 4;
arr = 3;
arr = 1;
arr = 5;

unordered_map < int, int >mp;

// Traverse through the given array
for (int i = 0; i < 5; i++){
int first = arr[i];
int sec = arr[i];

if (mp.find (sec) != mp.end () && mp[sec] == first)
cout << "(" << sec << ", " << first << ") ";

else			// Else put sec element of this pair in hash
mp[first] = sec;
}

return 0;

}

```

### Output

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