Finding symmetric pairs in an array in Python

Symmetric pairs in array in Python

In this page you will find the program to print all symmetric pairs in an array in python programming language. We are given with an array and need to print the all symmetric pairs present in the given array.

symmetric pairs in an array in Python

Algorithm :

  • Create a set.
  • Start iterating over pairs.
  • Insert the pair in set
  • Check if(y, x) exist in the set,
  • If yes, then print that element.

Code in Python

# Function to find all pairs that are a mirror of each other
def findPairs(pairs):
    # create an empty set of strings
    s = set()
    # do for each pair
    for (x, y) in pairs:
        # insert the current pair `(x, y)` into the set
        s.add((x, y))
        # if mirror pair `(y, x)` is seen before, print the pairs
        if (y, x) in s:
            print((x, y))

pairs = [(3, 4), (1, 2), (5, 2), (7, 10), (4, 3), (2, 5)]


(4, 3)
(2, 5)