C# Getting most popular combination of 2 items in collection












2















I have a collection of items, here it is:



    ID    ELECT1        ELECT2
1 FDI AED
2 BPG AED
3 AED FDI
4 FDI AED
5 GPH AED
6 AED BPG
7 GPH FDI


And I would like to group elect1 and elect2 and than output the 2 most popular so like
These items are chosen 3 times



FDI AED 
AED FDI
FDI AED


And then the second most popular combination is



AED BPG
BPG AED


So the output would be



2 Most popular combinations are
FDI AED
AED BPG


I have written some code but I don't know how I could possibly do this



var groups = cStudent.GroupBy(x => new { x.elective1, x.elective2 });    
foreach (var group in groups)
{
Console.WriteLine("{0} / {1}:", group.Key.elective1, group.Key.elective2);
foreach (var item in group)
{
Console.WriteLine(" {0} ({1})", item.elective1, item.elective2);
}
}


So this is what I get with my code it is halfway there just don't know how to finish.
IMAGE










share|improve this question

























  • Aren't items 1 and 4 the same?

    – Hogan
    Nov 22 '18 at 19:35
















2















I have a collection of items, here it is:



    ID    ELECT1        ELECT2
1 FDI AED
2 BPG AED
3 AED FDI
4 FDI AED
5 GPH AED
6 AED BPG
7 GPH FDI


And I would like to group elect1 and elect2 and than output the 2 most popular so like
These items are chosen 3 times



FDI AED 
AED FDI
FDI AED


And then the second most popular combination is



AED BPG
BPG AED


So the output would be



2 Most popular combinations are
FDI AED
AED BPG


I have written some code but I don't know how I could possibly do this



var groups = cStudent.GroupBy(x => new { x.elective1, x.elective2 });    
foreach (var group in groups)
{
Console.WriteLine("{0} / {1}:", group.Key.elective1, group.Key.elective2);
foreach (var item in group)
{
Console.WriteLine(" {0} ({1})", item.elective1, item.elective2);
}
}


So this is what I get with my code it is halfway there just don't know how to finish.
IMAGE










share|improve this question

























  • Aren't items 1 and 4 the same?

    – Hogan
    Nov 22 '18 at 19:35














2












2








2








I have a collection of items, here it is:



    ID    ELECT1        ELECT2
1 FDI AED
2 BPG AED
3 AED FDI
4 FDI AED
5 GPH AED
6 AED BPG
7 GPH FDI


And I would like to group elect1 and elect2 and than output the 2 most popular so like
These items are chosen 3 times



FDI AED 
AED FDI
FDI AED


And then the second most popular combination is



AED BPG
BPG AED


So the output would be



2 Most popular combinations are
FDI AED
AED BPG


I have written some code but I don't know how I could possibly do this



var groups = cStudent.GroupBy(x => new { x.elective1, x.elective2 });    
foreach (var group in groups)
{
Console.WriteLine("{0} / {1}:", group.Key.elective1, group.Key.elective2);
foreach (var item in group)
{
Console.WriteLine(" {0} ({1})", item.elective1, item.elective2);
}
}


So this is what I get with my code it is halfway there just don't know how to finish.
IMAGE










share|improve this question
















I have a collection of items, here it is:



    ID    ELECT1        ELECT2
1 FDI AED
2 BPG AED
3 AED FDI
4 FDI AED
5 GPH AED
6 AED BPG
7 GPH FDI


And I would like to group elect1 and elect2 and than output the 2 most popular so like
These items are chosen 3 times



FDI AED 
AED FDI
FDI AED


And then the second most popular combination is



AED BPG
BPG AED


So the output would be



2 Most popular combinations are
FDI AED
AED BPG


I have written some code but I don't know how I could possibly do this



var groups = cStudent.GroupBy(x => new { x.elective1, x.elective2 });    
foreach (var group in groups)
{
Console.WriteLine("{0} / {1}:", group.Key.elective1, group.Key.elective2);
foreach (var item in group)
{
Console.WriteLine(" {0} ({1})", item.elective1, item.elective2);
}
}


So this is what I get with my code it is halfway there just don't know how to finish.
IMAGE







c# list linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 3:06









Progressive

8641023




8641023










asked Nov 22 '18 at 18:55









T.RyanT.Ryan

182




182













  • Aren't items 1 and 4 the same?

    – Hogan
    Nov 22 '18 at 19:35



















  • Aren't items 1 and 4 the same?

    – Hogan
    Nov 22 '18 at 19:35

















Aren't items 1 and 4 the same?

– Hogan
Nov 22 '18 at 19:35





Aren't items 1 and 4 the same?

– Hogan
Nov 22 '18 at 19:35












3 Answers
3






active

oldest

votes


















3














IMHO, the cleanest way to solve this is implementing a custom equality comparer that provides the needed equality semantics to solve your current problem:



class IgnoreElectiveOrderStudentEqualityComparer 
: IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
=> (x.ElectiveOne == y.ElectiveOne &&
x.ElectiveTwo == y.ElectiveTwo) ||
(x.ElectiveOne == y.ElectiveTwo &&
x.ElectiveTwo == y.ElectiveOne);

public int GetHashCode(Student obj)
=> obj.ElectiveOne.GetHashCode() ^
obj.ElectiveTwo.GetHashCode();
}


And now you simply use GroupBy and the custom comparer:



var mostPopular = 
students.GroupBy(s => s,
new IgnoreElectiveOrderStudentEqualityComparer())
.OrderByDescending(g => g.Count())
.Select(g => new
{
g.Key.ElectiveOne,
g.Key.ElectiveTwo
})
.Take(2);





share|improve this answer


























  • You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

    – Hogan
    Nov 24 '18 at 15:37



















2














The trick is to have the order of the two electives of not matter so the alpha first one is always first (thus if you have (math, art) it is the same as (art, math):



var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0) 
return(new Tuple(x.elective1.x.elective2);
else
return(new Tuple(x.elective2,x.elective1);
},
(b, a) => new { Key = b, Count = a.Count() })
.OrderyByDesending(x => x.Count).Take(2);


You could add a select of the key if you don't want an object with the count.






share|improve this answer

































    0














    This should work for you:



    cStudent
    .Select(x => new { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
    .GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
    .OrderByDescending(x => x.Count)
    .Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
    .Take(2)





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436744%2fc-sharp-getting-most-popular-combination-of-2-items-in-collection%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      IMHO, the cleanest way to solve this is implementing a custom equality comparer that provides the needed equality semantics to solve your current problem:



      class IgnoreElectiveOrderStudentEqualityComparer 
      : IEqualityComparer<Student>
      {
      public bool Equals(Student x, Student y)
      => (x.ElectiveOne == y.ElectiveOne &&
      x.ElectiveTwo == y.ElectiveTwo) ||
      (x.ElectiveOne == y.ElectiveTwo &&
      x.ElectiveTwo == y.ElectiveOne);

      public int GetHashCode(Student obj)
      => obj.ElectiveOne.GetHashCode() ^
      obj.ElectiveTwo.GetHashCode();
      }


      And now you simply use GroupBy and the custom comparer:



      var mostPopular = 
      students.GroupBy(s => s,
      new IgnoreElectiveOrderStudentEqualityComparer())
      .OrderByDescending(g => g.Count())
      .Select(g => new
      {
      g.Key.ElectiveOne,
      g.Key.ElectiveTwo
      })
      .Take(2);





      share|improve this answer


























      • You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

        – Hogan
        Nov 24 '18 at 15:37
















      3














      IMHO, the cleanest way to solve this is implementing a custom equality comparer that provides the needed equality semantics to solve your current problem:



      class IgnoreElectiveOrderStudentEqualityComparer 
      : IEqualityComparer<Student>
      {
      public bool Equals(Student x, Student y)
      => (x.ElectiveOne == y.ElectiveOne &&
      x.ElectiveTwo == y.ElectiveTwo) ||
      (x.ElectiveOne == y.ElectiveTwo &&
      x.ElectiveTwo == y.ElectiveOne);

      public int GetHashCode(Student obj)
      => obj.ElectiveOne.GetHashCode() ^
      obj.ElectiveTwo.GetHashCode();
      }


      And now you simply use GroupBy and the custom comparer:



      var mostPopular = 
      students.GroupBy(s => s,
      new IgnoreElectiveOrderStudentEqualityComparer())
      .OrderByDescending(g => g.Count())
      .Select(g => new
      {
      g.Key.ElectiveOne,
      g.Key.ElectiveTwo
      })
      .Take(2);





      share|improve this answer


























      • You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

        – Hogan
        Nov 24 '18 at 15:37














      3












      3








      3







      IMHO, the cleanest way to solve this is implementing a custom equality comparer that provides the needed equality semantics to solve your current problem:



      class IgnoreElectiveOrderStudentEqualityComparer 
      : IEqualityComparer<Student>
      {
      public bool Equals(Student x, Student y)
      => (x.ElectiveOne == y.ElectiveOne &&
      x.ElectiveTwo == y.ElectiveTwo) ||
      (x.ElectiveOne == y.ElectiveTwo &&
      x.ElectiveTwo == y.ElectiveOne);

      public int GetHashCode(Student obj)
      => obj.ElectiveOne.GetHashCode() ^
      obj.ElectiveTwo.GetHashCode();
      }


      And now you simply use GroupBy and the custom comparer:



      var mostPopular = 
      students.GroupBy(s => s,
      new IgnoreElectiveOrderStudentEqualityComparer())
      .OrderByDescending(g => g.Count())
      .Select(g => new
      {
      g.Key.ElectiveOne,
      g.Key.ElectiveTwo
      })
      .Take(2);





      share|improve this answer















      IMHO, the cleanest way to solve this is implementing a custom equality comparer that provides the needed equality semantics to solve your current problem:



      class IgnoreElectiveOrderStudentEqualityComparer 
      : IEqualityComparer<Student>
      {
      public bool Equals(Student x, Student y)
      => (x.ElectiveOne == y.ElectiveOne &&
      x.ElectiveTwo == y.ElectiveTwo) ||
      (x.ElectiveOne == y.ElectiveTwo &&
      x.ElectiveTwo == y.ElectiveOne);

      public int GetHashCode(Student obj)
      => obj.ElectiveOne.GetHashCode() ^
      obj.ElectiveTwo.GetHashCode();
      }


      And now you simply use GroupBy and the custom comparer:



      var mostPopular = 
      students.GroupBy(s => s,
      new IgnoreElectiveOrderStudentEqualityComparer())
      .OrderByDescending(g => g.Count())
      .Select(g => new
      {
      g.Key.ElectiveOne,
      g.Key.ElectiveTwo
      })
      .Take(2);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 22 '18 at 19:38

























      answered Nov 22 '18 at 19:16









      InBetweenInBetween

      25.3k34068




      25.3k34068













      • You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

        – Hogan
        Nov 24 '18 at 15:37



















      • You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

        – Hogan
        Nov 24 '18 at 15:37

















      You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

      – Hogan
      Nov 24 '18 at 15:37





      You're right -- I miss read your code, of course XOR is an order independent operator. (I saw concatenation for some reason.)

      – Hogan
      Nov 24 '18 at 15:37













      2














      The trick is to have the order of the two electives of not matter so the alpha first one is always first (thus if you have (math, art) it is the same as (art, math):



      var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0) 
      return(new Tuple(x.elective1.x.elective2);
      else
      return(new Tuple(x.elective2,x.elective1);
      },
      (b, a) => new { Key = b, Count = a.Count() })
      .OrderyByDesending(x => x.Count).Take(2);


      You could add a select of the key if you don't want an object with the count.






      share|improve this answer






























        2














        The trick is to have the order of the two electives of not matter so the alpha first one is always first (thus if you have (math, art) it is the same as (art, math):



        var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0) 
        return(new Tuple(x.elective1.x.elective2);
        else
        return(new Tuple(x.elective2,x.elective1);
        },
        (b, a) => new { Key = b, Count = a.Count() })
        .OrderyByDesending(x => x.Count).Take(2);


        You could add a select of the key if you don't want an object with the count.






        share|improve this answer




























          2












          2








          2







          The trick is to have the order of the two electives of not matter so the alpha first one is always first (thus if you have (math, art) it is the same as (art, math):



          var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0) 
          return(new Tuple(x.elective1.x.elective2);
          else
          return(new Tuple(x.elective2,x.elective1);
          },
          (b, a) => new { Key = b, Count = a.Count() })
          .OrderyByDesending(x => x.Count).Take(2);


          You could add a select of the key if you don't want an object with the count.






          share|improve this answer















          The trick is to have the order of the two electives of not matter so the alpha first one is always first (thus if you have (math, art) it is the same as (art, math):



          var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0) 
          return(new Tuple(x.elective1.x.elective2);
          else
          return(new Tuple(x.elective2,x.elective1);
          },
          (b, a) => new { Key = b, Count = a.Count() })
          .OrderyByDesending(x => x.Count).Take(2);


          You could add a select of the key if you don't want an object with the count.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 19:34

























          answered Nov 22 '18 at 19:19









          HoganHogan

          54.9k866102




          54.9k866102























              0














              This should work for you:



              cStudent
              .Select(x => new { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
              .GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
              .OrderByDescending(x => x.Count)
              .Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
              .Take(2)





              share|improve this answer




























                0














                This should work for you:



                cStudent
                .Select(x => new { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
                .GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
                .OrderByDescending(x => x.Count)
                .Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
                .Take(2)





                share|improve this answer


























                  0












                  0








                  0







                  This should work for you:



                  cStudent
                  .Select(x => new { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
                  .GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
                  .OrderByDescending(x => x.Count)
                  .Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
                  .Take(2)





                  share|improve this answer













                  This should work for you:



                  cStudent
                  .Select(x => new { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
                  .GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
                  .OrderByDescending(x => x.Count)
                  .Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
                  .Take(2)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 19:10









                  Xiaoy312Xiaoy312

                  11.4k12133




                  11.4k12133






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436744%2fc-sharp-getting-most-popular-combination-of-2-items-in-collection%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Redirect URL with Chrome Remote Debugging Android Devices

                      Dieringhausen