Issues with lists

Kalyan A 355 Reputation points
2025-01-31T13:32:05.79+00:00

I am trying to work on HCF

I created a list with prime factors and I am trying to load the common factors

Parent=16 ,Number=2 , Cnts=4

Parent=48 ,Number=2 , Cnts=4

Parent=48 ,Number=3 , Cnts=1

Should give

Parent=16 ,Number=2 , Cnts=4

Parent=48 ,Number=2 , Cnts=4

As Number 2 exits between 2 parents.

Here is code snippet for Copilot it works well for one scenario it fail for another scenario

Parent=16 ,Number=2 , Cnts=4

Parent=48 ,Number=2 , Cnts=4

Parent=48 ,Number=3 , Cnts=1

Parent=7 ,Number=7 , Cnts=1

Here it should give empty result as parent 7 doesnt have number 2

public class Summary
{
    public int Parent { get; set; }
    public int Number { get; set; }
    public int Cnts { get; set; }
}
public class Program
{
    public static void Main(string[] args)
    {
        var summaries = new List<Summary>
        {
            new Summary { Parent = 16, Number = 2, Cnts = 4 },
            new Summary { Parent = 48, Number = 2, Cnts = 4 },
            new Summary { Parent = 48, Number = 3, Cnts = 1 },
            new Summary { Parent = 7, Number = 7, Cnts = 1 }
        };
        var filteredSummaries = summaries
            .GroupBy(s => s.Number)
            .Where(g => g.Select(s => s.Parent).Distinct().Count() > 1)
            .SelectMany(g => g)
            .ToList();
        Console.WriteLine("Filtered summaries:");
        foreach (var summary in filteredSummaries)
        {
            Console.WriteLine($"Parent: {summary.Parent}, Number: {summary.Number}, Cnts: {summary.Cnts}");
        }
        if (!filteredSummaries.Any())
        {
            Console.WriteLine("The list is empty.");
        }
    }
}
// Should give Empty 
//Where as Sample 2 
using System;
using System.Collections.Generic;
using System.Linq;
public class Summary
{
    public int Parent { get; set; }
    public int Number { get; set; }
    public int Cnts { get; set; }
}
public class Program
{
    public static void Main(string[] args)
    {
        var summaries = new List<Summary>
        {
            new Summary { Parent = 16, Number = 2, Cnts = 4 },
            new Summary { Parent = 48, Number = 2, Cnts = 4 },
            new Summary { Parent = 48, Number = 3, Cnts = 1 }, 
        };
        var filteredSummaries = summaries
            .GroupBy(s => s.Number)
            .Where(g => g.Select(s => s.Parent).Distinct().Count() > 1)
            .SelectMany(g => g)
            .ToList();
        Console.WriteLine("Filtered summaries:");
        foreach (var summary in filteredSummaries)
        {
            Console.WriteLine($"Parent: {summary.Parent}, Number: {summary.Number}, Cnts: {summary.Cnts}");
        }
        if (!filteredSummaries.Any())
        {
            Console.WriteLine("The list is empty.");
        }
    }
}
/*
should give 
Filtered summaries:
Parent: 16, Number: 2, Cnts: 4
Parent: 48, Number: 2, Cnts: 4
*/
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,279 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 119.6K Reputation points
    2025-01-31T14:25:22.2866667+00:00

    Try an alternative:

    var parents = summaries
        .Select( s => s.Parent )
        .Distinct( )
        .ToList( );
    var filteredSummaries = summaries
        .Where( s => parents.All( p => summaries.Any( q => q.Parent == p && q.Number == s.Number ) ) )
        .ToList( );
    
    0 comments No comments

  2. Kalyan A 355 Reputation points
    2025-01-31T18:52:22.3466667+00:00

    I found some hits in Copilot finished the code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    public class Summary
    {
        public int Parent { get; set; }
        public int Number { get; set; }
        public int Cnts { get; set; }
    }
         
    public class Program
    {
      
        public static void Main(string[] args)
        {
           
        var summaries = new List<Summary>
            {
                new Summary { Parent = 60, Number = 2, Cnts = 2 },
                new Summary { Parent = 60, Number = 3, Cnts = 1},
                new Summary { Parent = 60, Number = 5, Cnts = 1},
                new Summary { Parent = 90, Number = 2, Cnts = 1 },
                new Summary { Parent = 90, Number = 3, Cnts = 2},
                new Summary { Parent = 90, Number = 5, Cnts = 1} 
            };
            // Create a dictionary to group summaries by Parent and Number
            var newSummaries = new List<Summary>(summaries);
            // Count the number of newSummaries where Parent is not equal to summaries.Parent
            // Count the number of distinct parents in newSummaries where Parent is not equal to summaries.Parent and Number matches
           
            var filtergroups = new List<Summary>();
            foreach (var summary in summaries)
            {
                int count = 0; int countm = 0;
                var distinctNewParents = newSummaries
                    .Where(ns => ns.Parent != summary.Parent)
                    .Select(ns => ns.Parent)
                    .Distinct();
                foreach (var parent in distinctNewParents)
                {
                    count++;
                }
            
                               //
                               var distinctParentsmatch = newSummaries
                        .Where(ns => ns.Parent != summary.Parent && ns.Number == summary.Number)
                        .Select(ns => ns.Parent)
                        .Distinct();
                var distinctParents = new HashSet<int>();
                foreach (var parentx in distinctParentsmatch)
                    {
                        distinctParents.Add(parentx);
                    }
                countm = distinctParents.Count;
                if (count == countm)
                {
                    filtergroups.Add(new Summary { Parent = summary.Parent, Number = summary.Number, Cnts = summary.Cnts });
                }
            }
            // Print the filtergroups
            if (filtergroups.Any())
            {
                Console.WriteLine("Filtered groups:");
                foreach (var fg in filtergroups)
                {
                    Console.WriteLine($"Parent: {fg.Parent}, Number: {fg.Number}, Cnts: {fg.Cnts}");
                }
            }
            else
            {
                Console.WriteLine("The list is empty.");
            }
        }
        
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.