Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i have below dict. & i have to sort this based on duplicate value of double[] so am looking for sorted list with all duplicates only
C#
Dictionary<int, double[]> counts1 = new Dictionary<int,double[]>();

Am trying to use this but it doesn't work out
C#
var duplicateValues = counts1.GroupBy(x => x.Value).Where(x => x.Count()> 1);

any leads appreciated thanks

What I have tried:

sort duplicate array values in dictionary

below is the dictionary
C#
Dictionary<int, double[]> counts1 = new Dictionary<int,double[]>();

below method trying to sort out
C#
var duplicateValues = counts1.GroupBy(x => x.Value).Where(x => x.Count()> 1);
Posted
Updated 24-Mar-24 23:38pm
v3
Comments
Graeme_Grant 25-Mar-24 5:34am    
post some sample data that needs to be sorted

In .NET, arrays are only considered "equal" if they are the same reference. Two different arrays containing the same values are not considered equal.

If you need to find duplicates amongst different arrays containing the same values, you'll need a custom equality comparer.
C#
public sealed class ArrayEqualityComparer<T> : IEqualityComparer<T[]>
{
    private readonly IEqualityComparer<T> _elementComparer;
    
    public ArrayEqualityComparer(IEqualityComparer<T> elementComparer)
    {
        _elementComparer = elementComparer ?? throw new ArgumentNullException(nameof(elementComparer));
    }
    
    public ArrayEqualityComparer()
    {
        _elementComparer = EqualityComparer<T>.Default;
    }
    
    public int GetHashCode(T[] obj)
    {
        if (obj is null || obj.Length == 0) return 0;
        
        HashCode code = new HashCode();
        for (int index = 0; index < obj.Length; index++)
        {
            T element = obj[index];
            code.Add(element, _elementComparer);
        }
        
        return code.ToHashCode();
    }
    
    public bool Equals(T[] x, T[] y)
    {
        if (x is null) return y is null;
        if (y is null) return false;
        if (x.Length != y.Length) return false;
        
        for (int index = 0; index < x.Length; index++)
        {
            T xElement = x[index];
            T yElement = y[index];
            if (!_elementComparer.Equals(xElement, yElement))
            {
                return false;
            }
        }
        
        return true;
    }
}
You will then need to pass an instance of that equality comparer to the GroupBy method:
C#
var duplicateValues = counts1.GroupBy(x => x.Value, new ArrayEqualityComparer<double>()).Where(x => x.Count()> 1);
 
Share this answer
 
v3
Comments
M-Badger 25-Mar-24 6:47am    
Should the GetHashCode method have a 'return code' line or does the compiler know what to do with that ?
Richard Deeming 25-Mar-24 6:49am    
Good spot! return code.ToHashCode(); added.
Member 14224038 25-Mar-24 8:50am    
thanks a lot for the code, but it doesnt workout for me. dont know why
while debug itonly goes to public ArrayEqualityComparer()
{
_elementComparer = EqualityComparer<t>.Default;
}

& return back with null. my double values have array of 6.
Richard Deeming 25-Mar-24 8:58am    
What does "return back with null" mean?
Richard Deeming 25-Mar-24 9:19am    
I've just tried the code, and it works as expected:
Duplicate Arrays | C# Online Compiler | .NET Fiddle[^]
I'm not sure what you want to achieve, but...
Suppose you want to sort dictionary values based on its occurence.

Here is an example:
C#
Dictionary<int, double[]> d = new Dictionary<int,double[]>()
{
	{1, new double[]{.5, 1.2, 3.4, .5, 1.2, 3.4, .5, 1.2, 3.4, .5, 1.2}},
	{2, new double[]{.5, 1.2, 3.4, .5, 1.2, 3.4, .5, 1.2, 3.4, .5, 1.2, 3.4, 3.4}},
	{3, new double[]{.5, 1.2, 3.4, .5, 1.2, 3.4}},
	{4, new double[]{.5, 1.2, 3.4, 3.4, 3.4, .5, 1.2, 3.4, .5, 3.4}},
	{5, new double[]{.5, 1.2, 3.4}}
};


Dictionary<int, Dictionary<double, int>> result = d
	.Select(x=> new KeyValuePair<int, Dictionary<double, int>>
		(x.Key, new Dictionary<double, int>(x.Value.GroupBy(y => y)
				.Select(g => new KeyValuePair<double, int>(g.Key, g.Count()))
				.OrderByDescending(k => k.Value)
				.ToDictionary(kvp => kvp.Key, kvp => kvp.Value))))
	.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);


Result:
Key    Values
1      0.5 | 4 
       1.2 | 4 
       3.4 | 3 
2      3.4 | 5 
       0,5 | 4 
       1,2 | 4 
and so on...
 
Share this answer
 
Hi,

refer this article will help you
Using Lookup For Duplicate Key Value Pairs Dictionary[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900