How to find items tied for most appearances in a list?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












I want to create a list of the items in list w tied for appearing the most times in w. In other words, if 22 appears in w more than any other number, I want the result to be 22. If 34 and 55 appear the most times in w but the same number of times, I want the result to be 34,55.



The example below just uses a randomly generated list w. My method works but is ugly and inefficient.



w = RandomInteger[100, 200]
fw = w // DeleteDuplicates
wc = Counts[w]
m = Max[fw /. wc]
Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]


Is there a tidier way?










share|improve this question



























    up vote
    8
    down vote

    favorite












    I want to create a list of the items in list w tied for appearing the most times in w. In other words, if 22 appears in w more than any other number, I want the result to be 22. If 34 and 55 appear the most times in w but the same number of times, I want the result to be 34,55.



    The example below just uses a randomly generated list w. My method works but is ugly and inefficient.



    w = RandomInteger[100, 200]
    fw = w // DeleteDuplicates
    wc = Counts[w]
    m = Max[fw /. wc]
    Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]


    Is there a tidier way?










    share|improve this question

























      up vote
      8
      down vote

      favorite









      up vote
      8
      down vote

      favorite











      I want to create a list of the items in list w tied for appearing the most times in w. In other words, if 22 appears in w more than any other number, I want the result to be 22. If 34 and 55 appear the most times in w but the same number of times, I want the result to be 34,55.



      The example below just uses a randomly generated list w. My method works but is ugly and inefficient.



      w = RandomInteger[100, 200]
      fw = w // DeleteDuplicates
      wc = Counts[w]
      m = Max[fw /. wc]
      Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]


      Is there a tidier way?










      share|improve this question















      I want to create a list of the items in list w tied for appearing the most times in w. In other words, if 22 appears in w more than any other number, I want the result to be 22. If 34 and 55 appear the most times in w but the same number of times, I want the result to be 34,55.



      The example below just uses a randomly generated list w. My method works but is ugly and inefficient.



      w = RandomInteger[100, 200]
      fw = w // DeleteDuplicates
      wc = Counts[w]
      m = Max[fw /. wc]
      Reap[Do[If[(fw[[i]] /. wc) == m, Sow[fw[[i]]]], i, 1, Length[fw]]][[2, 1]]


      Is there a tidier way?







      list-manipulation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 11 at 8:55

























      asked Sep 11 at 0:55









      Jerry Guern

      1,954833




      1,954833




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          You need Commonest:



          SeedRandom[1]
          w = RandomInteger[100, 200];

          Commonest[w]



          68, 25, 63




          % /. Counts[w]



          5, 5, 5







          share|improve this answer






















          • Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
            – Jerry Guern
            Sep 11 at 4:21






          • 1




            @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
            – kglr
            Sep 11 at 4:38











          • Okay, thanks for the followup and links.
            – Jerry Guern
            Sep 11 at 8:51










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fmathematica.stackexchange.com%2fquestions%2f181652%2fhow-to-find-items-tied-for-most-appearances-in-a-list%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          9
          down vote



          accepted










          You need Commonest:



          SeedRandom[1]
          w = RandomInteger[100, 200];

          Commonest[w]



          68, 25, 63




          % /. Counts[w]



          5, 5, 5







          share|improve this answer






















          • Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
            – Jerry Guern
            Sep 11 at 4:21






          • 1




            @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
            – kglr
            Sep 11 at 4:38











          • Okay, thanks for the followup and links.
            – Jerry Guern
            Sep 11 at 8:51














          up vote
          9
          down vote



          accepted










          You need Commonest:



          SeedRandom[1]
          w = RandomInteger[100, 200];

          Commonest[w]



          68, 25, 63




          % /. Counts[w]



          5, 5, 5







          share|improve this answer






















          • Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
            – Jerry Guern
            Sep 11 at 4:21






          • 1




            @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
            – kglr
            Sep 11 at 4:38











          • Okay, thanks for the followup and links.
            – Jerry Guern
            Sep 11 at 8:51












          up vote
          9
          down vote



          accepted







          up vote
          9
          down vote



          accepted






          You need Commonest:



          SeedRandom[1]
          w = RandomInteger[100, 200];

          Commonest[w]



          68, 25, 63




          % /. Counts[w]



          5, 5, 5







          share|improve this answer














          You need Commonest:



          SeedRandom[1]
          w = RandomInteger[100, 200];

          Commonest[w]



          68, 25, 63




          % /. Counts[w]



          5, 5, 5








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 11 at 6:24

























          answered Sep 11 at 1:00









          kglr

          170k8193397




          170k8193397











          • Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
            – Jerry Guern
            Sep 11 at 4:21






          • 1




            @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
            – kglr
            Sep 11 at 4:38











          • Okay, thanks for the followup and links.
            – Jerry Guern
            Sep 11 at 8:51
















          • Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
            – Jerry Guern
            Sep 11 at 4:21






          • 1




            @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
            – kglr
            Sep 11 at 4:38











          • Okay, thanks for the followup and links.
            – Jerry Guern
            Sep 11 at 8:51















          Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
          – Jerry Guern
          Sep 11 at 4:21




          Thank you! The first part is exactly what I need. But you could explain how the syntax of the second part works? I don't even know how to look that up.
          – Jerry Guern
          Sep 11 at 4:21




          1




          1




          @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
          – kglr
          Sep 11 at 4:38





          @JerryGuern, thank you for the accept. About the last line: it gives the same result as 68, 25, 63 /. Counts[w]. % stands for the last result generated . (See Out (%) in the docs.) So the last line is the same as Count[w, #]&/@ 68,25,63. See also: Map (/@), Function (&) and Slot (#).
          – kglr
          Sep 11 at 4:38













          Okay, thanks for the followup and links.
          – Jerry Guern
          Sep 11 at 8:51




          Okay, thanks for the followup and links.
          – Jerry Guern
          Sep 11 at 8:51

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181652%2fhow-to-find-items-tied-for-most-appearances-in-a-list%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Carbon dioxide

          Why am i infinitely getting the same tweet with the Twitter Search API?