Most frequent and least duplicate frequent number in array












-2















I'm trying to find the most frequent number and the least frequent duplicate number in an array E.g



[7, 5, 6, 4, 6, 5, 5, 8, 7, 0, 7, 5, 2, 9, 7, 9, 3, 4, 6]



These are the duplicate number in the array above:




  1. "7" appears (4 times).

  2. "5" appears (4 times).

  3. "6" appears (3 times).

  4. "4" appears (2 times).


"7" and "5" are the most frequent number,
"4" is the least frequent duplicate.



When I tried to code, I was able to get the number 7, but then I don't know how to implement the least frequent.



This is the code I wrote:



String numbers = "7564655870752979346".split("");
String elements = "";
int count = 0;
for (String tempElement : numbers) {
int tempCount = 0;
for (n = 0; n < numbers.length; n++) {
if (numbers[n].equals(tempElement)) {
tempCount++;
if (tempCount > count) {
elements = tempElement;
// System.out.println(elements);
count = tempCount;
}
}
}
}
System.out.println("Frequent number is: " + elements + " It appeared " + count+" times");


My solution above only prints out 7, and I don't know how to check for the least duplicate.










share|improve this question





























    -2















    I'm trying to find the most frequent number and the least frequent duplicate number in an array E.g



    [7, 5, 6, 4, 6, 5, 5, 8, 7, 0, 7, 5, 2, 9, 7, 9, 3, 4, 6]



    These are the duplicate number in the array above:




    1. "7" appears (4 times).

    2. "5" appears (4 times).

    3. "6" appears (3 times).

    4. "4" appears (2 times).


    "7" and "5" are the most frequent number,
    "4" is the least frequent duplicate.



    When I tried to code, I was able to get the number 7, but then I don't know how to implement the least frequent.



    This is the code I wrote:



    String numbers = "7564655870752979346".split("");
    String elements = "";
    int count = 0;
    for (String tempElement : numbers) {
    int tempCount = 0;
    for (n = 0; n < numbers.length; n++) {
    if (numbers[n].equals(tempElement)) {
    tempCount++;
    if (tempCount > count) {
    elements = tempElement;
    // System.out.println(elements);
    count = tempCount;
    }
    }
    }
    }
    System.out.println("Frequent number is: " + elements + " It appeared " + count+" times");


    My solution above only prints out 7, and I don't know how to check for the least duplicate.










    share|improve this question



























      -2












      -2








      -2








      I'm trying to find the most frequent number and the least frequent duplicate number in an array E.g



      [7, 5, 6, 4, 6, 5, 5, 8, 7, 0, 7, 5, 2, 9, 7, 9, 3, 4, 6]



      These are the duplicate number in the array above:




      1. "7" appears (4 times).

      2. "5" appears (4 times).

      3. "6" appears (3 times).

      4. "4" appears (2 times).


      "7" and "5" are the most frequent number,
      "4" is the least frequent duplicate.



      When I tried to code, I was able to get the number 7, but then I don't know how to implement the least frequent.



      This is the code I wrote:



      String numbers = "7564655870752979346".split("");
      String elements = "";
      int count = 0;
      for (String tempElement : numbers) {
      int tempCount = 0;
      for (n = 0; n < numbers.length; n++) {
      if (numbers[n].equals(tempElement)) {
      tempCount++;
      if (tempCount > count) {
      elements = tempElement;
      // System.out.println(elements);
      count = tempCount;
      }
      }
      }
      }
      System.out.println("Frequent number is: " + elements + " It appeared " + count+" times");


      My solution above only prints out 7, and I don't know how to check for the least duplicate.










      share|improve this question
















      I'm trying to find the most frequent number and the least frequent duplicate number in an array E.g



      [7, 5, 6, 4, 6, 5, 5, 8, 7, 0, 7, 5, 2, 9, 7, 9, 3, 4, 6]



      These are the duplicate number in the array above:




      1. "7" appears (4 times).

      2. "5" appears (4 times).

      3. "6" appears (3 times).

      4. "4" appears (2 times).


      "7" and "5" are the most frequent number,
      "4" is the least frequent duplicate.



      When I tried to code, I was able to get the number 7, but then I don't know how to implement the least frequent.



      This is the code I wrote:



      String numbers = "7564655870752979346".split("");
      String elements = "";
      int count = 0;
      for (String tempElement : numbers) {
      int tempCount = 0;
      for (n = 0; n < numbers.length; n++) {
      if (numbers[n].equals(tempElement)) {
      tempCount++;
      if (tempCount > count) {
      elements = tempElement;
      // System.out.println(elements);
      count = tempCount;
      }
      }
      }
      }
      System.out.println("Frequent number is: " + elements + " It appeared " + count+" times");


      My solution above only prints out 7, and I don't know how to check for the least duplicate.







      java arrays duplicates frequency






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 18 '18 at 21:57







      king amada

















      asked Nov 24 '18 at 2:34









      king amadaking amada

      349




      349
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Write a function to find Min Repeated number(started from 2)



          public static String findMin(String numbers, int counter) {
          int count = 0;
          String elements = "";
          for (String tempElement : numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
          if (numbers[n].equals(tempElement)) {
          tempCount++;
          if (tempCount > counter) {
          count = 0;
          break;
          }
          if (tempCount > count) {
          elements = tempElement;
          // System.out.println(elements);
          count = tempCount;
          }
          }
          }
          if(count == counter) {
          return elements;
          }
          }
          if(count < counter) {
          return "";
          }
          return elements;
          }


          A loop over numbers:



          String x = "";
          int c = 2;
          do {
          x = findMin(numbers, c ++);
          } while(x == "");


          The whole code will be



          public class X {

          public static String findMin(String numbers, int counter) {
          int count = 0;
          String elements = "";
          for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
          if (numbers[n].equals(tempElement)) {
          tempCount++;
          if (tempCount > counter) {
          count = 0;
          break;
          }
          if (tempCount > count) {
          elements = tempElement;
          // System.out.println(elements);
          count = tempCount;
          }
          }
          }
          if (count == counter) {
          return elements;
          }
          }
          if (count < counter) {
          return "";
          }
          return elements;
          }

          public static void main(String args) {
          String numbers = "756655874075297346".split("");
          String elements = "";
          int count = 0;
          for (String tempElement: numbers) {
          int tempCount = 0;
          for (int n = 0; n < numbers.length; n++) {
          if (numbers[n].equals(tempElement)) {
          tempCount++;
          if (tempCount > count) {
          elements = tempElement;
          // System.out.println(elements);
          count = tempCount;
          }
          }
          }
          }
          String x = "";
          int c = 2;
          do {
          x = findMin(numbers, c++);
          } while (x == "");

          System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");

          System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
          }
          }





          share|improve this answer
























          • Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

            – king amada
            Nov 24 '18 at 6:06











          • For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

            – Saeed.Ataee
            Nov 24 '18 at 6:38



















          0














          Solution:




          1. Count the number of times each digit appears. Use an array of counts, one for each distinct digit.

          2. Find the position in the array of counts with count == 2.


          Implementation ... is for you to do. But you will need to find a way to convert a String containing one digit into an integer. (Hint: search the javadocs!)






          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%2f53454707%2fmost-frequent-and-least-duplicate-frequent-number-in-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Write a function to find Min Repeated number(started from 2)



            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement : numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if(count == counter) {
            return elements;
            }
            }
            if(count < counter) {
            return "";
            }
            return elements;
            }


            A loop over numbers:



            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c ++);
            } while(x == "");


            The whole code will be



            public class X {

            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if (count == counter) {
            return elements;
            }
            }
            if (count < counter) {
            return "";
            }
            return elements;
            }

            public static void main(String args) {
            String numbers = "756655874075297346".split("");
            String elements = "";
            int count = 0;
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            }
            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c++);
            } while (x == "");

            System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");

            System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
            }
            }





            share|improve this answer
























            • Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

              – king amada
              Nov 24 '18 at 6:06











            • For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

              – Saeed.Ataee
              Nov 24 '18 at 6:38
















            1














            Write a function to find Min Repeated number(started from 2)



            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement : numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if(count == counter) {
            return elements;
            }
            }
            if(count < counter) {
            return "";
            }
            return elements;
            }


            A loop over numbers:



            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c ++);
            } while(x == "");


            The whole code will be



            public class X {

            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if (count == counter) {
            return elements;
            }
            }
            if (count < counter) {
            return "";
            }
            return elements;
            }

            public static void main(String args) {
            String numbers = "756655874075297346".split("");
            String elements = "";
            int count = 0;
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            }
            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c++);
            } while (x == "");

            System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");

            System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
            }
            }





            share|improve this answer
























            • Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

              – king amada
              Nov 24 '18 at 6:06











            • For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

              – Saeed.Ataee
              Nov 24 '18 at 6:38














            1












            1








            1







            Write a function to find Min Repeated number(started from 2)



            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement : numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if(count == counter) {
            return elements;
            }
            }
            if(count < counter) {
            return "";
            }
            return elements;
            }


            A loop over numbers:



            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c ++);
            } while(x == "");


            The whole code will be



            public class X {

            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if (count == counter) {
            return elements;
            }
            }
            if (count < counter) {
            return "";
            }
            return elements;
            }

            public static void main(String args) {
            String numbers = "756655874075297346".split("");
            String elements = "";
            int count = 0;
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            }
            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c++);
            } while (x == "");

            System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");

            System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
            }
            }





            share|improve this answer













            Write a function to find Min Repeated number(started from 2)



            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement : numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if(count == counter) {
            return elements;
            }
            }
            if(count < counter) {
            return "";
            }
            return elements;
            }


            A loop over numbers:



            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c ++);
            } while(x == "");


            The whole code will be



            public class X {

            public static String findMin(String numbers, int counter) {
            int count = 0;
            String elements = "";
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > counter) {
            count = 0;
            break;
            }
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            if (count == counter) {
            return elements;
            }
            }
            if (count < counter) {
            return "";
            }
            return elements;
            }

            public static void main(String args) {
            String numbers = "756655874075297346".split("");
            String elements = "";
            int count = 0;
            for (String tempElement: numbers) {
            int tempCount = 0;
            for (int n = 0; n < numbers.length; n++) {
            if (numbers[n].equals(tempElement)) {
            tempCount++;
            if (tempCount > count) {
            elements = tempElement;
            // System.out.println(elements);
            count = tempCount;
            }
            }
            }
            }
            String x = "";
            int c = 2;
            do {
            x = findMin(numbers, c++);
            } while (x == "");

            System.out.println("Frequent number is: " + elements + " It appeared " + count + " times");

            System.out.println("Min Frequent number is: " + x + " It appeared " + (c - 1) + " times");
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 5:17









            Saeed.AtaeeSaeed.Ataee

            3,65811531




            3,65811531













            • Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

              – king amada
              Nov 24 '18 at 6:06











            • For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

              – Saeed.Ataee
              Nov 24 '18 at 6:38



















            • Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

              – king amada
              Nov 24 '18 at 6:06











            • For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

              – Saeed.Ataee
              Nov 24 '18 at 6:38

















            Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

            – king amada
            Nov 24 '18 at 6:06





            Thanks for the answer, but when I tried to use this number "3620035681863138685" It prints out 3 as the most frequent, whilst "8" and "6" appeared as many times as the "3". How can I then make all the highest frequency appear and I select the one that is higher in the case of the above number, 8 would higher as it's higher than 6 and 3.

            – king amada
            Nov 24 '18 at 6:06













            For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

            – Saeed.Ataee
            Nov 24 '18 at 6:38





            For that case, The solution I suggest is to count all numbers' Repetition and store them in an array and then choose the numbers you want from that array. @kingamada

            – Saeed.Ataee
            Nov 24 '18 at 6:38













            0














            Solution:




            1. Count the number of times each digit appears. Use an array of counts, one for each distinct digit.

            2. Find the position in the array of counts with count == 2.


            Implementation ... is for you to do. But you will need to find a way to convert a String containing one digit into an integer. (Hint: search the javadocs!)






            share|improve this answer






























              0














              Solution:




              1. Count the number of times each digit appears. Use an array of counts, one for each distinct digit.

              2. Find the position in the array of counts with count == 2.


              Implementation ... is for you to do. But you will need to find a way to convert a String containing one digit into an integer. (Hint: search the javadocs!)






              share|improve this answer




























                0












                0








                0







                Solution:




                1. Count the number of times each digit appears. Use an array of counts, one for each distinct digit.

                2. Find the position in the array of counts with count == 2.


                Implementation ... is for you to do. But you will need to find a way to convert a String containing one digit into an integer. (Hint: search the javadocs!)






                share|improve this answer















                Solution:




                1. Count the number of times each digit appears. Use an array of counts, one for each distinct digit.

                2. Find the position in the array of counts with count == 2.


                Implementation ... is for you to do. But you will need to find a way to convert a String containing one digit into an integer. (Hint: search the javadocs!)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 24 '18 at 3:06

























                answered Nov 24 '18 at 2:59









                Stephen CStephen C

                516k70565921




                516k70565921






























                    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%2f53454707%2fmost-frequent-and-least-duplicate-frequent-number-in-array%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

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...