Home > front end >  Matching and Joining Lists by common elements
Matching and Joining Lists by common elements

Time:04-19

The Situation is : I have a huge number of lists with a fixed size of 3 elements, for a group of four of those lists, there will be 4 unique elements that make up these lists. Example:

Elements = [A,B,C,D,E,F,G,H]
Lists:
List 1 = {A,B,C}
List 2 = {B,C,D}
List 3 = {C,D,A}
List 4 = {D,A,B}
List 5 = {E,F,G}
List 6 = {F,G,H}
List 7 = {G,H,E}
List 8 = {H,E,F}

So, in this example elements [A, B, C, D] made four lists of three elements per list. Here comes the question: how can i match these four lists to get a single list that Contain [A, B, C, D]

CodePudding user response:

Using modular arithmetic and sum will work. If you divide the index of the item by 4 and sum the 3 values you will be able to group by this number.

    class Program
    {
 
        static void Main(string[] args)
        {
            List<string> Elements = new List<string>() {"A","B","C","D","E","F","G","H"};
            MyClass[][] lists = {
                                  new MyClass[] {new MyClass() { name = "A", value = 0},new MyClass() { name = "B", value = 0},new MyClass() { name = "C", value = 0}},
                                  new MyClass[] {new MyClass() { name = "B", value = 0},new MyClass() { name = "C", value = 0},new MyClass() { name = "D", value = 0}},
                                  new MyClass[] {new MyClass() { name = "C", value = 0},new MyClass() { name = "D", value = 0},new MyClass() { name = "A", value = 0}},
                                  new MyClass[] {new MyClass() { name = "D", value = 0},new MyClass() { name = "A", value = 0},new MyClass() { name = "B", value = 0}},
                                  new MyClass[] {new MyClass() { name = "E", value = 0},new MyClass() { name = "F", value = 0},new MyClass() { name = "G", value = 0}},
                                  new MyClass[] {new MyClass() { name = "F", value = 0},new MyClass() { name = "G", value = 0},new MyClass() { name = "H", value = 0}},
                                  new MyClass[] {new MyClass() { name = "G", value = 0},new MyClass() { name = "H", value = 0},new MyClass() { name = "E", value = 0}},
                                  new MyClass[] {new MyClass() { name = "H", value = 0},new MyClass() { name = "E", value = 0},new MyClass() { name = "F", value = 0}},
                              };

            var results = lists.Select(x => new { sum = (x.Select(y => Elements.IndexOf(y.name)/4).Sum()), values = x })
                .GroupBy(x => x.sum)
                .ToDictionary(x => x.Key, y => y);
        }
    }
    public class MyClass
    {
        public string name { get; set; }
        public int value { get; set; }
    }

CodePudding user response:

I think your best bet would be to make use of a hashset:

HashSet<char> uniqueValues = new HashSet<char>();

char[] list1 = new char[] { 'A', 'B', 'C' };
char[] list2 = new char[] { 'B', 'C', 'D' };
char[] list3 = new char[] { 'C', 'D', 'A' };
char[] list4 = new char[] { 'D', 'A', 'B' };

var lists = new char[][] { list1, list2, list3, list4 };

foreach (var list in lists)
{
    foreach (var letter in list)
    {
        uniqueValues.Add(letter);
    }
}

Console.WriteLine(string.Join(", ", uniqueValues));

//  A, B, C, D

Hashsets specifically don't record duplicate values, so no matter how many times you add the letter A to your hashset, it'll only appear once. That seems to be exactly what you're looking for.

  •  Tags:  
  • c#
  • Related