Home > Software design >  How to get unique duplicate values from an list object c#
How to get unique duplicate values from an list object c#

Time:12-24

Is there a way to get unique duplicated values from a list object. I have tried the following and I get an error to rewrite my query. I am not sure if it is an EF limitation or I am overlooking some issue. My Goal is to return unique duplicates from a list OBJECT that contain mixed records some of which are duplicates.

var UniqueDuplicates = MYList()
      .GroupBy(i => new { i.item })
      .Where(i => i.Count() > 1)
      .SelectMany(x => x)
      .OrderBy(i => i.SubmittedOn)                                      
      .ToList();
var people = new[]
{
    new {
            Name = "Vernon",Age = 24,Email = "[email protected]",Phone = "806-291-8721",          
        },
    new {
            Name = "Carrie",Age = 24,Email = "[email protected]",Phone = "617-389-2329",
            
        },
    new {
            Name = "Thomas",Age = 23, Email = "[email protected]", Phone = "906-875-5259",
        }
    new{
            Name = "kerry",Age = 22, Email = "[email protected]", Phone = "906-875-5259",
        }
    new{
            Name = "Tim",Age = 23, Email = "[email protected]", Phone = "906-875-5259",
        }
    new{
            Name = "Neil",Age = 23, Email = "[email protected]", Phone = "906-875-5259",
        }
        
};

the below method return the unique count of duplicates for the value, in my list it will return 2

  var countduplicates = People().GroupBy(x => x.age).Count(x => x.Count() > 1);

any help is greatly appreciated.

CodePudding user response:

If you have a method* that can return the count of appearances of an object within a list, something like this could do the trick (partly pseudocode):

private List<string> ReturnUniqueDuplicates(List<string> InputList)
        {
            List<string> Duplicates = new List<string>();
            foreach (string Element in InputList)
            {
                if (object appears more than once* and not in Duplicates ) { Duplicates.Add(Element); }
            }
            return Duplicates;
        }

However, if you're having trouble getting the count, it seems someone had a really nice solution to that here: A method to count occurrences in a list

CodePudding user response:

So you have a collection of People:

var people = new[] {
  new { Name = "Vernon", Age = 24, Email = "[email protected]", Phone = "806-291-8721", },
  new { Name = "Carrie", Age = 24, Email = "[email protected]", Phone = "617-389-2329", },
  new { Name = "Thomas", Age = 23, Email = "[email protected]", Phone = "906-875-5259", },
  new { Name = "kerry",  Age = 22, Email = "[email protected]",  Phone = "906-875-5259", },
  new { Name = "Tim",    Age = 23, Email = "[email protected]",    Phone = "906-875-5259", },
  new { Name = "Neil",   Age = 23, Email = "[email protected]",   Phone = "906-875-5259", },
};

And you are grouping people by their Age, and so you have:

Age: 22, Count: 1
Age: 23, Count: 3
Age: 24, Count: 2

Finally, as I can see (correct me if I am not right), you want to get unique Ages with unique Count which is greater than 1. We have 2 distinct Counts greater than 1 (these are 2 and 3) so the answer is {23, 24}.

If it's your case, you can put it as:

var result = people
  .GroupBy(human => human.Age)
  .Select(group => (
     age   : group.Key, 
     count : group.Count())
   )
  .Where(item => item.count > 1)  // Duplicates only
  .DistinctBy(item => item.count) // .Net 6 option 
  .Select(item => item.age)       // We want just ages  
  .ToArray();                     // Let it be materialized as an array   

If you have an old version of .Net / C#, you can use GroupBy First instead of DistinctBy:

var result = people
  .GroupBy(human => human.Age)
  .Select(group => (
     age   : group.Key, 
     count : group.Count())
   )
  .Where(item => item.count > 1)  
  .GroupBy(item => item.count) 
  .Select(group => group.First().age) 
  .ToArray();                        

Please Fiddle yourself

  •  Tags:  
  • c#
  • Related