score:12

Accepted answer

this returns the sequence you look for:

var result = mylist
    .select(s => s.split('-').orderby(s1 => s1))
    .select(a => string.join("-", a.toarray()))
    .distinct();

foreach (var str in result)
{
    console.writeline(str);
}

in short: split each string on the - character into two-element arrays. sort each array, and join them back together. then you can simply use distinct to get the unique values.

update: when thinking a bit more, i realized that you can easily remove one of the select calls:

var result = mylist
    .select(s => string.join("-", s.split('-').orderby(s1 => s1).toarray()))
    .distinct();

disclaimer: this solution will always keep the value "a-b" over "b-a", regardless of the order in which the appear in the original sequence.

score:-2

int checkid = 0;
while (checkid < mylist.count)
{
 string szcheckitem = mylist[checkid];
 string []pairs = szcheckitem.split("-".tochararray());
 string szinvertitem = pairs[1] + "-" + pairs[0];
 int i=checkid+1;
 while (i < mylist.count)
 {
  if((mylist[i] == szcheckitem) || (mylist[i] == szinvertitem))
  {
   mylist.removeat(i);
   continue;
  }
  i++;
 }

 checkid++;
}

score:1

very basic, but could be written better (but it's just working):

class comparer : iequalitycomparer<string>
  {
      public bool equals(string x, string y)
      {
          return (x[0] == y[0] && x[2] == y[2]) || (x[0] == y[2] && x[2] == y[0]);
      }

      public int gethashcode(string obj)
      {
          return 0;
      }
  }

var mylist = new list<string>
{ 
    "a-b", 
    "b-a", 
    "c-d", 
    "c-e", 
    "d-c",
    "d-e",
    "e-c",
    "e-d",
    "f-g",
    "g-f"
}
.distinct(new comparer());

foreach (var s in mylist)
{
    console.writeline(s);
}

score:1

you need to implement the iequalitycomparer like this:

public class charcomparer : iequalitycomparer<string>
{
    #region iequalitycomparer<string> members

    public bool equals(string x, string y)
    {
        if (x == y)
            return true;

        if (x.length == 3 && y.length == 3)
        {
            if (x[2] == y[0] && x[0] == y[2])
                return true;

            if (x[0] == y[2] && x[2] == y[0])
                return true;
        }

        return false;
    }

    public int gethashcode(string obj)
    {
        // return 0 to force the equals to fire (otherwise it won't...!)
        return 0;
    }

    #endregion
}

the sample program:

class program
{
    static void main(string[] args)
    {
        list<string> mylist = new list<string>
        { 
            "a-b", 
            "b-a", 
            "c-d", 
            "c-e", 
            "d-c",
            "d-e",
            "e-c",
            "e-d",
            "f-g",
            "g-f"
        };

        var distinct = mylist.distinct(new charcomparer());
        foreach (string s in distinct)
            console.writeline(s);

        console.readline();
    }
}

the result:

"a-b"   
"c-d"
"c-e"   
"d-e"
"f-g"

score:4

you can use the enumerable.distinct(ienumerable<tsource>, iequalitycomparer<tsource>) overload.

now you just need to implement iequalitycomparer. here's something for you to get started:

class comparer : iequalitycomparer<string>
{

    public bool equals(string s1, string s2)
    {
        // will need to test for nullity
        return reverse(s1).equals(s2);
    }

    public int gethashcode(string s)
    {
        // will have to implement this
    }

}

for a reverse() implementation, see this question

score:14

implement iequalitycomparer witch returns true on equals("a-b", "b-a"). and use enumerable.distinct method


Related Query

More Query from same tag