score:12
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
Source: stackoverflow.com
Related Query
- How to remove duplicate combinations from a List<string> using LINQ
- How to remove duplicates from collection using IEqualityComparer, LinQ Distinct
- How do I remove items from generic list, based on multiple conditions and using linq
- How to remove characters from a string using LINQ
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to find duplicate record using Linq from DataTable
- How to remove duplicates from an Array using LINQ
- How to remove substring from all strings in a list in C# using LINQ
- How can i remove the duplicate element from a xml document without scribbling the structure of xml using c#
- How to remove an item from ListView using LINQ in C#
- Remove Back to Back Duplicate Entries from a List using Linq Method Syntax
- how to remove objects from list by multiple variables using linq
- How to remove any value from Dictionary<string,List<string>> using LINQ
- Using LINQ and EF, how to remove values from database where not in list of items
- How can I remove duplicate, invalid, child nodes from an XML document using Linq to XML?
- How to remove duplicates from SQLite DB - using ENtity and LINQ
- How to remove duplicate records from LINQ query
- How to reinsert data from one table onto itself using LINQ in code migration?
- Remove both duplicate items (if one is duplicate) from a list using Linq
- How to read date from text file, assign it to date and remove duplicate date in C# Linq
- How to remove duplicate elements in a list using LINQ based on a custom condition?
- Remove duplicate objects from a list using LINQ in c#
- remove a duplicate element(with specific value) from xml using linq
- How to remove items from multidimensional List using LINQ
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Using linq how do i remove multiple records from a cross reference table
- How to remove duplicates from a List of List using Linq
- Using LINQ to remove elements from a List<T>
- How to get duplicate items from a list using LINQ?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
More Query from same tag
- What is from keyword in linq?
- Collection was modified exception while enumerating in new subcollection
- VB.net Linq join on two columns
- VB.NET Search for objects in list and parse values of found object to array
- Change loops on 2D List to LINQ
- comparing 2 lists of objects and return changes in the new list
- HashSet or Distinct to read distinct values of property in List<> of objects
- Convert Query to Lambda Expression
- How to AND multiple query filters together in a loop dynamically? MongoDB C#
- LINQ expression duplicates values on select instead of cycling them
- gridview databind with linq query errors
- How to toggle EntityFramework Tracker using boolean variable
- Return array of key=>value pair using Lambda in C#
- LINQ Grouping help
- find non intersecting data set with linq
- What is a clean LINQ query to get items with no child items?
- Join two tables with key in common and show on a datagrid c# linq
- LINQ to SQL throws SQL Exception using local collection
- LINQ joining values from different classes
- How to access associations in a LINQ query?
- Checking for Linq Variables
- Filter SelectListItem value using some condition
- How do I Iterate Linq group result set?
- linq to entity using two tables
- Converting IQueryable<object> results to comma delimited string
- Difference between IList<T> and List<T>
- c# sorting a List<> using Tuple?
- Explanation of how this lambda expression in plain English?
- Cannot insert the value NULL into column ... Exception issues
- c# WPF selected item from listbox bound to datatable