I’m looking for a way to get distinct values from a jagged array. I’ve tried putting it in a dictionary, but it doesn’t seem to see the values as distinct. Anyone any ideas for a solution? Here is what I have so far:
int[][] connectionList = GetArray();
Sample data returned from this:
[0][1, 130, 136, 138]
[1][1, 78, 126, 138]
[2][1, 10, 125, 138]
[3][1, 130, 136, 138]
[4][1, 78, 126, 138]
[5][1, 130, 136, 138]
[6][1, 72, 135, 138]
[7][1, 73, 135, 138]
[8][1, 130, 136, 138]
Have tried adding to a dictionary. I tried to catch duplicate values as they are added, but that didn’t work, so tried to add .Distinct(), but no joy there either
Dictionary<int, int[]> myDictionary = new Dictionary<int, int[]>();
for (int i = 0; i < connectionList.Length; i++)
{
List<int> list = new List<int>();
for (int j = 0; j < connectionList[i].Length; j++)
{
list.Add(connectionList[i][j]);
}
if (myDictionary.Where(x => x.Value == list.ToArray()).Count() == 0)
myDictionary.Add(i, list.ToArray());
}
var distinctList = myDictionary.Values.Distinct().ToList();
From the above list, the output I’m looking for would be:
[0][1, 130, 136, 138]
[1][1, 78, 126, 138]
[2][1, 10, 125, 138]
[4][1, 72, 135, 138]
[5][1, 73, 135, 138]
Any thoughts how I could achieve this?
Here’s one way to do it:
Although it’s probably preferable to create a custom
IEqualityComparer<T>— as suggested by Crab Bucket and Tobias — rather than creating a throwaway string to use for the comparison.