Check if multiple elements in an array contain the same value












0















For instance I have an array that gets filled with random numbers and am going to call this one dice.



Random rnd = new Random()
int dice=new int [5]
for (int i=0;i<dice.length;i++)
{
dice[i]= rnd.next(1,7)
}


Now for the sake of simplicity I wanna ask how can I find out if got a three of a kind of instance.










share|improve this question

























  • int dice=new dice [5] not sure if this compiles

    – Hanjun Chen
    Nov 25 '18 at 22:17











  • @HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

    – Abdul SH
    Nov 26 '18 at 21:57
















0















For instance I have an array that gets filled with random numbers and am going to call this one dice.



Random rnd = new Random()
int dice=new int [5]
for (int i=0;i<dice.length;i++)
{
dice[i]= rnd.next(1,7)
}


Now for the sake of simplicity I wanna ask how can I find out if got a three of a kind of instance.










share|improve this question

























  • int dice=new dice [5] not sure if this compiles

    – Hanjun Chen
    Nov 25 '18 at 22:17











  • @HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

    – Abdul SH
    Nov 26 '18 at 21:57














0












0








0








For instance I have an array that gets filled with random numbers and am going to call this one dice.



Random rnd = new Random()
int dice=new int [5]
for (int i=0;i<dice.length;i++)
{
dice[i]= rnd.next(1,7)
}


Now for the sake of simplicity I wanna ask how can I find out if got a three of a kind of instance.










share|improve this question
















For instance I have an array that gets filled with random numbers and am going to call this one dice.



Random rnd = new Random()
int dice=new int [5]
for (int i=0;i<dice.length;i++)
{
dice[i]= rnd.next(1,7)
}


Now for the sake of simplicity I wanna ask how can I find out if got a three of a kind of instance.







c# arrays algorithm random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 20:04







Abdul SH

















asked Nov 25 '18 at 20:54









Abdul SHAbdul SH

84




84













  • int dice=new dice [5] not sure if this compiles

    – Hanjun Chen
    Nov 25 '18 at 22:17











  • @HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

    – Abdul SH
    Nov 26 '18 at 21:57



















  • int dice=new dice [5] not sure if this compiles

    – Hanjun Chen
    Nov 25 '18 at 22:17











  • @HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

    – Abdul SH
    Nov 26 '18 at 21:57

















int dice=new dice [5] not sure if this compiles

– Hanjun Chen
Nov 25 '18 at 22:17





int dice=new dice [5] not sure if this compiles

– Hanjun Chen
Nov 25 '18 at 22:17













@HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

– Abdul SH
Nov 26 '18 at 21:57





@HanjunChen Well in my original code I have dice as a class therefore my array was like so (public Dice dices = new Dice[5];) and that's why I made this VERY MINOR mistake writing the questions. Yet the ONLY thing you've seen in the questions is this! So helpful really

– Abdul SH
Nov 26 '18 at 21:57












3 Answers
3






active

oldest

votes


















2














use a IDictionary<int,int>



var dict = new Dictionary<int,int>();
foreach (int i in dice)
if(!dict.ContainsKey(i))
dict.Add(i,1);
else dict[i]++;


(optional) you can use Linq to get the numbers that appear multiple times



var duplicates = dict.Where( x=>x.Value > 1 )
.Select(x=>x.Key)
.ToList();





share|improve this answer

































    1














        // preparation (basically your code)
    var rnd = new Random();
    var dice = new int[5];

    for (int i=0; i < dice.Length; i++)
    {
    dice[i]= rnd.Next(1,7);
    }

    // select dices, grouped by with their count
    var groupedByCount = dice.GroupBy(d => d, d => 1 /* each hit counts as 1 */);

    // show all dices with their count
    foreach (var g in groupedByCount)
    Console.WriteLine(g.Key + ": " + g.Count());

    // show the dices with 3 or more
    foreach (var g in groupedByCount.Where(g => g.Count() >= 3))
    Console.WriteLine("3 times or more: " + g.Key);





    share|improve this answer































      0














      To give a completely different approach, instead of:



      Random rnd = new Random();
      int dice=new int[5];
      for (int i=0;i<dice.length;i++)
      {
      dice[i]= rnd.next(1,7);
      }


      Try this:



      Random rnd = new Random();
      int valueCount = new int[6];
      for (int i=0; i<5; i++)
      {
      valueCount[rnd.next(0,6)]++;
      }

      //you have kept track of each value.
      if (valueCount.Any(c => c == 3))
      //3 of a kind


      Of course you can combine both....



      Do note that this works for a really specific rule engine, optimized for counting events.



      If you really want a card/dice game, you'll need to rethink the rule engine to coupe with rules like "is it: 1,2,3,4,5,6, and in that order?".



      For that, try: How to implement a rule engine?






      share|improve this answer


























      • But you never update the valueCount array.

        – Jim Mischel
        Nov 26 '18 at 14:10











      • @JimMischel: .... oops, my bad... copy/past issue

        – Stefan
        Nov 26 '18 at 14:12











      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%2f53471881%2fcheck-if-multiple-elements-in-an-array-contain-the-same-value%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









      2














      use a IDictionary<int,int>



      var dict = new Dictionary<int,int>();
      foreach (int i in dice)
      if(!dict.ContainsKey(i))
      dict.Add(i,1);
      else dict[i]++;


      (optional) you can use Linq to get the numbers that appear multiple times



      var duplicates = dict.Where( x=>x.Value > 1 )
      .Select(x=>x.Key)
      .ToList();





      share|improve this answer






























        2














        use a IDictionary<int,int>



        var dict = new Dictionary<int,int>();
        foreach (int i in dice)
        if(!dict.ContainsKey(i))
        dict.Add(i,1);
        else dict[i]++;


        (optional) you can use Linq to get the numbers that appear multiple times



        var duplicates = dict.Where( x=>x.Value > 1 )
        .Select(x=>x.Key)
        .ToList();





        share|improve this answer




























          2












          2








          2







          use a IDictionary<int,int>



          var dict = new Dictionary<int,int>();
          foreach (int i in dice)
          if(!dict.ContainsKey(i))
          dict.Add(i,1);
          else dict[i]++;


          (optional) you can use Linq to get the numbers that appear multiple times



          var duplicates = dict.Where( x=>x.Value > 1 )
          .Select(x=>x.Key)
          .ToList();





          share|improve this answer















          use a IDictionary<int,int>



          var dict = new Dictionary<int,int>();
          foreach (int i in dice)
          if(!dict.ContainsKey(i))
          dict.Add(i,1);
          else dict[i]++;


          (optional) you can use Linq to get the numbers that appear multiple times



          var duplicates = dict.Where( x=>x.Value > 1 )
          .Select(x=>x.Key)
          .ToList();






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 22:28

























          answered Nov 25 '18 at 22:21









          Hanjun ChenHanjun Chen

          34128




          34128

























              1














                  // preparation (basically your code)
              var rnd = new Random();
              var dice = new int[5];

              for (int i=0; i < dice.Length; i++)
              {
              dice[i]= rnd.Next(1,7);
              }

              // select dices, grouped by with their count
              var groupedByCount = dice.GroupBy(d => d, d => 1 /* each hit counts as 1 */);

              // show all dices with their count
              foreach (var g in groupedByCount)
              Console.WriteLine(g.Key + ": " + g.Count());

              // show the dices with 3 or more
              foreach (var g in groupedByCount.Where(g => g.Count() >= 3))
              Console.WriteLine("3 times or more: " + g.Key);





              share|improve this answer




























                1














                    // preparation (basically your code)
                var rnd = new Random();
                var dice = new int[5];

                for (int i=0; i < dice.Length; i++)
                {
                dice[i]= rnd.Next(1,7);
                }

                // select dices, grouped by with their count
                var groupedByCount = dice.GroupBy(d => d, d => 1 /* each hit counts as 1 */);

                // show all dices with their count
                foreach (var g in groupedByCount)
                Console.WriteLine(g.Key + ": " + g.Count());

                // show the dices with 3 or more
                foreach (var g in groupedByCount.Where(g => g.Count() >= 3))
                Console.WriteLine("3 times or more: " + g.Key);





                share|improve this answer


























                  1












                  1








                  1







                      // preparation (basically your code)
                  var rnd = new Random();
                  var dice = new int[5];

                  for (int i=0; i < dice.Length; i++)
                  {
                  dice[i]= rnd.Next(1,7);
                  }

                  // select dices, grouped by with their count
                  var groupedByCount = dice.GroupBy(d => d, d => 1 /* each hit counts as 1 */);

                  // show all dices with their count
                  foreach (var g in groupedByCount)
                  Console.WriteLine(g.Key + ": " + g.Count());

                  // show the dices with 3 or more
                  foreach (var g in groupedByCount.Where(g => g.Count() >= 3))
                  Console.WriteLine("3 times or more: " + g.Key);





                  share|improve this answer













                      // preparation (basically your code)
                  var rnd = new Random();
                  var dice = new int[5];

                  for (int i=0; i < dice.Length; i++)
                  {
                  dice[i]= rnd.Next(1,7);
                  }

                  // select dices, grouped by with their count
                  var groupedByCount = dice.GroupBy(d => d, d => 1 /* each hit counts as 1 */);

                  // show all dices with their count
                  foreach (var g in groupedByCount)
                  Console.WriteLine(g.Key + ": " + g.Count());

                  // show the dices with 3 or more
                  foreach (var g in groupedByCount.Where(g => g.Count() >= 3))
                  Console.WriteLine("3 times or more: " + g.Key);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 21:06









                  WaescherWaescher

                  2,85431730




                  2,85431730























                      0














                      To give a completely different approach, instead of:



                      Random rnd = new Random();
                      int dice=new int[5];
                      for (int i=0;i<dice.length;i++)
                      {
                      dice[i]= rnd.next(1,7);
                      }


                      Try this:



                      Random rnd = new Random();
                      int valueCount = new int[6];
                      for (int i=0; i<5; i++)
                      {
                      valueCount[rnd.next(0,6)]++;
                      }

                      //you have kept track of each value.
                      if (valueCount.Any(c => c == 3))
                      //3 of a kind


                      Of course you can combine both....



                      Do note that this works for a really specific rule engine, optimized for counting events.



                      If you really want a card/dice game, you'll need to rethink the rule engine to coupe with rules like "is it: 1,2,3,4,5,6, and in that order?".



                      For that, try: How to implement a rule engine?






                      share|improve this answer


























                      • But you never update the valueCount array.

                        – Jim Mischel
                        Nov 26 '18 at 14:10











                      • @JimMischel: .... oops, my bad... copy/past issue

                        – Stefan
                        Nov 26 '18 at 14:12
















                      0














                      To give a completely different approach, instead of:



                      Random rnd = new Random();
                      int dice=new int[5];
                      for (int i=0;i<dice.length;i++)
                      {
                      dice[i]= rnd.next(1,7);
                      }


                      Try this:



                      Random rnd = new Random();
                      int valueCount = new int[6];
                      for (int i=0; i<5; i++)
                      {
                      valueCount[rnd.next(0,6)]++;
                      }

                      //you have kept track of each value.
                      if (valueCount.Any(c => c == 3))
                      //3 of a kind


                      Of course you can combine both....



                      Do note that this works for a really specific rule engine, optimized for counting events.



                      If you really want a card/dice game, you'll need to rethink the rule engine to coupe with rules like "is it: 1,2,3,4,5,6, and in that order?".



                      For that, try: How to implement a rule engine?






                      share|improve this answer


























                      • But you never update the valueCount array.

                        – Jim Mischel
                        Nov 26 '18 at 14:10











                      • @JimMischel: .... oops, my bad... copy/past issue

                        – Stefan
                        Nov 26 '18 at 14:12














                      0












                      0








                      0







                      To give a completely different approach, instead of:



                      Random rnd = new Random();
                      int dice=new int[5];
                      for (int i=0;i<dice.length;i++)
                      {
                      dice[i]= rnd.next(1,7);
                      }


                      Try this:



                      Random rnd = new Random();
                      int valueCount = new int[6];
                      for (int i=0; i<5; i++)
                      {
                      valueCount[rnd.next(0,6)]++;
                      }

                      //you have kept track of each value.
                      if (valueCount.Any(c => c == 3))
                      //3 of a kind


                      Of course you can combine both....



                      Do note that this works for a really specific rule engine, optimized for counting events.



                      If you really want a card/dice game, you'll need to rethink the rule engine to coupe with rules like "is it: 1,2,3,4,5,6, and in that order?".



                      For that, try: How to implement a rule engine?






                      share|improve this answer















                      To give a completely different approach, instead of:



                      Random rnd = new Random();
                      int dice=new int[5];
                      for (int i=0;i<dice.length;i++)
                      {
                      dice[i]= rnd.next(1,7);
                      }


                      Try this:



                      Random rnd = new Random();
                      int valueCount = new int[6];
                      for (int i=0; i<5; i++)
                      {
                      valueCount[rnd.next(0,6)]++;
                      }

                      //you have kept track of each value.
                      if (valueCount.Any(c => c == 3))
                      //3 of a kind


                      Of course you can combine both....



                      Do note that this works for a really specific rule engine, optimized for counting events.



                      If you really want a card/dice game, you'll need to rethink the rule engine to coupe with rules like "is it: 1,2,3,4,5,6, and in that order?".



                      For that, try: How to implement a rule engine?







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 26 '18 at 14:12

























                      answered Nov 25 '18 at 21:00









                      StefanStefan

                      8,52873761




                      8,52873761













                      • But you never update the valueCount array.

                        – Jim Mischel
                        Nov 26 '18 at 14:10











                      • @JimMischel: .... oops, my bad... copy/past issue

                        – Stefan
                        Nov 26 '18 at 14:12



















                      • But you never update the valueCount array.

                        – Jim Mischel
                        Nov 26 '18 at 14:10











                      • @JimMischel: .... oops, my bad... copy/past issue

                        – Stefan
                        Nov 26 '18 at 14:12

















                      But you never update the valueCount array.

                      – Jim Mischel
                      Nov 26 '18 at 14:10





                      But you never update the valueCount array.

                      – Jim Mischel
                      Nov 26 '18 at 14:10













                      @JimMischel: .... oops, my bad... copy/past issue

                      – Stefan
                      Nov 26 '18 at 14:12





                      @JimMischel: .... oops, my bad... copy/past issue

                      – Stefan
                      Nov 26 '18 at 14:12


















                      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%2f53471881%2fcheck-if-multiple-elements-in-an-array-contain-the-same-value%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python