C# get distinct values from jagged array
-
04-12-2019 - |
質問
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:
var distinctList = connectionList.GroupBy(x => string.Join(",", x))
.Select(g => g.First())
.ToList();
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.
他のヒント
This will be easy with a LINQ Distinct
, all you need is to provide your own implementation for IEqualityComparer
:
public class IntArrayComparer : IEqualityComparer<int[]>
{
public bool Equals(int[] i1, int[] i2)
{
if(ReferenceEquals(i1, i2))
{
return true;
}
else if(i1 == null || i2 == null)
{
return false;
}
else if(i1.Length != i2.Length)
{
return false;
}
for(int i = 0; i < i1.Length; ++i)
{
if(i1[i] != i2[i]) return false;
}
return true;
}
public int GetHashCode(int[] obj)
{
// Average is probably not the best hash for an int array,
// but I'm lazy right now and this is only for demonstration purposes
return obj != null ? (int)obj.Average() : 0;
}
}
And use it in your code, like this:
int[][] connectionList = GetArray().Distinct(new IntArrayComparer()).ToArray();
Could you user IEqualityComparer
public class MyComparer : IEqualityComparer<int []>
{
bool IEqualityComparer<int[]>.Equals(int[] x, int[] y)
{
//.. your particular comparison logic goes here
}
int IEqualityComparer<int[]>.GetHashCode(int [] obj)
{
return obj.GetHashCode();
}
}
Then call like this
var distinctList = myDictionary.Values.Distinct(new MyComparer()).ToList();