Question: I have two lists of different lengths and i want match the items based on their actual relation. One list is the secondary structure elements and other list is aligned sequence. I want to match the secondary structure to its residues in the other list. And adjust the length of secondary structure by inserting '-' to that of gaps in the aligned sequence. The items in ss corresponds to RRCAVVTG in seq.

```
ss=['-', '-', 'E', 'E', 'E', 'E', 'S', 'S']
seq≈["---------------RRCAVVTG"]
for m in seq:
        found=[i for i in list(m)]
sscount=0
sscount1=0
for  char,ssi in zip(found,ss): 

    if char!='-'  :
        print char , sscount, ssi
        sscount+=1

    else:
        print char, sscount1, '#'
        sscount1+=1

```

The expected results:

```
---------------##EEEESS

---------------RRCAVVTG

```

But i get the following results:

```
 - 0 #
 - 1 #
 - 2 #
 - 3 #
 - 4 #
 - 5 #
 - 6 #
 - 7 #

```

Answer: 
class `EqualityComparerExtensions` that will return a value indicating if two edges are neighbors.

```
static class EqualityComparerExtensions
{
    internal static bool Neighbours<T>(this IEqualityComparer<T> comparer, 
        Tuple<T, T> a, Tuple<T, T> b)
    {
        return comparer.Equals(a.Item1, b.Item1) 
            || comparer.Equals(a.Item1, b.Item2)
            || comparer.Equals(a.Item2, b.Item1) 
            || comparer.Equals(a.Item2, b.Item2);
    }
}

```

Then the algorithm would be:

```
public static T[] GetVerticesFromEdges<T>(Tuple<T, T>[] edges, 
    IEqualityComparer<T> comparer)
{
    var array = edges.Clone() as Tuple<T, T>[];
    var last = array.Length - 1;
    for (int i = 0; i < last; i++)
    {
        var c = 0;
        for (int j = i + 1; j < array.Length; j++)
        {
            if (comparer.Neighbours(array[i], array[j]))
            {
                var t = array[i + 1];
                array[i + 1] = array[j];
                array[j] = t;
                c++;
            }
        }
        if (c > 2 || c == 0)
        {
            throw new ArgumentException($"{nameof(edges)} is not a Polygon!");
        }
    }
    if (!comparer.Neighbours(array[last], array[0]))
    {
        throw new ArgumentException($"{nameof(edges)} is not a Polygon!");
    }
    for (int i = 0, j = 1; j < array.Length; i++, j++)
    {
        if (!comparer.Equals(array[i].Item2, array[j].Item1))
        {
            if (comparer.Equals(array[i].Item2, array[j].Item2))
            {
                array[j] = Tuple.Create(array[j].Item2, array[j].Item1);
            }
            else
            {
                array[i] = Tuple.Create(array[i].Item2, array[i].Item1);
            }
        }
    }
    if (!comparer.Equals(array[last].Item2, array[0].Item1))
    {
        throw new ArgumentException($"{nameof(edges)} is not a Polygon!");
    }
    return array.Select(a => a.Item1).ToArray();
}

```
You trying to find connected components of an undirected graph. Strictly speaking, not connected components in general, but one component special case.

You can find more about them in [Wikipedia page](https://en.wikipedia.org/wiki/Component_(graph_theory)) and/or take a look at an example [implementation in C#](https://www.geeksforgeeks.org/connected-components-in-an-undirected-graph/)