Get the most frequent number and least frequent duplicate in array











up vote
1
down vote

favorite












This code gets the most frequent number and least duplicate number in an array. How can I optimize it and improve its performance?



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 question




























    up vote
    1
    down vote

    favorite












    This code gets the most frequent number and least duplicate number in an array. How can I optimize it and improve its performance?



    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 question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      This code gets the most frequent number and least duplicate number in an array. How can I optimize it and improve its performance?



      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 question















      This code gets the most frequent number and least duplicate number in an array. How can I optimize it and improve its performance?



      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");
      }
      }






      java performance array statistics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      200_success

      127k15148411




      127k15148411










      asked 2 days ago









      king amada

      296




      296






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted











          • First, you should care about indentation.

          • Why slitting the numbers string while you can access individual char with numbers.charAt(n) ? It force you to work with String instead of char.

          • You don't check for validity of the string. Here, it's hard coded, but if you have to get it from a unknown source, a good habit is to validate it before using.

          • Also, you don't check if there's duplicates or not. If numbers is "1234567890" your program go for a infinite loop.

          • You compute the "Max" count into the main, and the "Min" in a function; try to be consistent.

          • For both computations, you make $n*n$ iterations (where $n$ is the numbers length) giving a complexity of $ O(n^2)$ for both.


          Instead, try to construct a table of occurrences by traversing the table once.
          After, simply search in this table the min and max values in one traversal.



          There's a very naive implementation, but i think a lot simpler to understand than your, and surely more efficient.



          public class X {
          public static void main(String args) {

          String data = "756655874075297346";
          int counts = new int[10];

          for (int i = 0; i < data.length(); i++) {
          char n = data.charAt(i);
          if (n >= '0' && n <= '9') {
          counts[n-'0']++;
          }
          }

          int min_index = 0;
          int max_index = 0;
          int min_count = Integer.MAX_VALUE;
          int max_count = 0;

          for (int i = 0; i < 10; i++) {
          if (counts[i] >= max_count) {
          max_index = i;
          max_count = counts[i];
          }
          if (counts[i] > 1 && counts[i] < min_count) {
          min_index = i;
          min_count = counts[i];
          }
          }
          System.out.println("Frequent number is: " + (char)(max_index + '0') + " It appeared " + max_count + " times");
          if (min_count < Integer.MAX_VALUE) {
          System.out.println("Min Frequent number is: " + (char)(min_index + '0') + " It appeared " + min_count + " times");
          }
          else {
          System.out.println("There's no duplicates!");
          }
          }
          }


          Here, the function print the higher number with the higher number of occurrences (in case of multiples char with maximal occurrence count). If instead you want to get the lower, change if (counts[i] >= max_count) for if (counts[i] > max_count).



          Conversely, it print the lower duplicated number with the lower count, to get the higher duplicated with the lower count, change counts[i] < min_count with counts[i] <= min_count.






          share|improve this answer























            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.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            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%2fcodereview.stackexchange.com%2fquestions%2f208352%2fget-the-most-frequent-number-and-least-frequent-duplicate-in-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted











            • First, you should care about indentation.

            • Why slitting the numbers string while you can access individual char with numbers.charAt(n) ? It force you to work with String instead of char.

            • You don't check for validity of the string. Here, it's hard coded, but if you have to get it from a unknown source, a good habit is to validate it before using.

            • Also, you don't check if there's duplicates or not. If numbers is "1234567890" your program go for a infinite loop.

            • You compute the "Max" count into the main, and the "Min" in a function; try to be consistent.

            • For both computations, you make $n*n$ iterations (where $n$ is the numbers length) giving a complexity of $ O(n^2)$ for both.


            Instead, try to construct a table of occurrences by traversing the table once.
            After, simply search in this table the min and max values in one traversal.



            There's a very naive implementation, but i think a lot simpler to understand than your, and surely more efficient.



            public class X {
            public static void main(String args) {

            String data = "756655874075297346";
            int counts = new int[10];

            for (int i = 0; i < data.length(); i++) {
            char n = data.charAt(i);
            if (n >= '0' && n <= '9') {
            counts[n-'0']++;
            }
            }

            int min_index = 0;
            int max_index = 0;
            int min_count = Integer.MAX_VALUE;
            int max_count = 0;

            for (int i = 0; i < 10; i++) {
            if (counts[i] >= max_count) {
            max_index = i;
            max_count = counts[i];
            }
            if (counts[i] > 1 && counts[i] < min_count) {
            min_index = i;
            min_count = counts[i];
            }
            }
            System.out.println("Frequent number is: " + (char)(max_index + '0') + " It appeared " + max_count + " times");
            if (min_count < Integer.MAX_VALUE) {
            System.out.println("Min Frequent number is: " + (char)(min_index + '0') + " It appeared " + min_count + " times");
            }
            else {
            System.out.println("There's no duplicates!");
            }
            }
            }


            Here, the function print the higher number with the higher number of occurrences (in case of multiples char with maximal occurrence count). If instead you want to get the lower, change if (counts[i] >= max_count) for if (counts[i] > max_count).



            Conversely, it print the lower duplicated number with the lower count, to get the higher duplicated with the lower count, change counts[i] < min_count with counts[i] <= min_count.






            share|improve this answer



























              up vote
              1
              down vote



              accepted











              • First, you should care about indentation.

              • Why slitting the numbers string while you can access individual char with numbers.charAt(n) ? It force you to work with String instead of char.

              • You don't check for validity of the string. Here, it's hard coded, but if you have to get it from a unknown source, a good habit is to validate it before using.

              • Also, you don't check if there's duplicates or not. If numbers is "1234567890" your program go for a infinite loop.

              • You compute the "Max" count into the main, and the "Min" in a function; try to be consistent.

              • For both computations, you make $n*n$ iterations (where $n$ is the numbers length) giving a complexity of $ O(n^2)$ for both.


              Instead, try to construct a table of occurrences by traversing the table once.
              After, simply search in this table the min and max values in one traversal.



              There's a very naive implementation, but i think a lot simpler to understand than your, and surely more efficient.



              public class X {
              public static void main(String args) {

              String data = "756655874075297346";
              int counts = new int[10];

              for (int i = 0; i < data.length(); i++) {
              char n = data.charAt(i);
              if (n >= '0' && n <= '9') {
              counts[n-'0']++;
              }
              }

              int min_index = 0;
              int max_index = 0;
              int min_count = Integer.MAX_VALUE;
              int max_count = 0;

              for (int i = 0; i < 10; i++) {
              if (counts[i] >= max_count) {
              max_index = i;
              max_count = counts[i];
              }
              if (counts[i] > 1 && counts[i] < min_count) {
              min_index = i;
              min_count = counts[i];
              }
              }
              System.out.println("Frequent number is: " + (char)(max_index + '0') + " It appeared " + max_count + " times");
              if (min_count < Integer.MAX_VALUE) {
              System.out.println("Min Frequent number is: " + (char)(min_index + '0') + " It appeared " + min_count + " times");
              }
              else {
              System.out.println("There's no duplicates!");
              }
              }
              }


              Here, the function print the higher number with the higher number of occurrences (in case of multiples char with maximal occurrence count). If instead you want to get the lower, change if (counts[i] >= max_count) for if (counts[i] > max_count).



              Conversely, it print the lower duplicated number with the lower count, to get the higher duplicated with the lower count, change counts[i] < min_count with counts[i] <= min_count.






              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted







                • First, you should care about indentation.

                • Why slitting the numbers string while you can access individual char with numbers.charAt(n) ? It force you to work with String instead of char.

                • You don't check for validity of the string. Here, it's hard coded, but if you have to get it from a unknown source, a good habit is to validate it before using.

                • Also, you don't check if there's duplicates or not. If numbers is "1234567890" your program go for a infinite loop.

                • You compute the "Max" count into the main, and the "Min" in a function; try to be consistent.

                • For both computations, you make $n*n$ iterations (where $n$ is the numbers length) giving a complexity of $ O(n^2)$ for both.


                Instead, try to construct a table of occurrences by traversing the table once.
                After, simply search in this table the min and max values in one traversal.



                There's a very naive implementation, but i think a lot simpler to understand than your, and surely more efficient.



                public class X {
                public static void main(String args) {

                String data = "756655874075297346";
                int counts = new int[10];

                for (int i = 0; i < data.length(); i++) {
                char n = data.charAt(i);
                if (n >= '0' && n <= '9') {
                counts[n-'0']++;
                }
                }

                int min_index = 0;
                int max_index = 0;
                int min_count = Integer.MAX_VALUE;
                int max_count = 0;

                for (int i = 0; i < 10; i++) {
                if (counts[i] >= max_count) {
                max_index = i;
                max_count = counts[i];
                }
                if (counts[i] > 1 && counts[i] < min_count) {
                min_index = i;
                min_count = counts[i];
                }
                }
                System.out.println("Frequent number is: " + (char)(max_index + '0') + " It appeared " + max_count + " times");
                if (min_count < Integer.MAX_VALUE) {
                System.out.println("Min Frequent number is: " + (char)(min_index + '0') + " It appeared " + min_count + " times");
                }
                else {
                System.out.println("There's no duplicates!");
                }
                }
                }


                Here, the function print the higher number with the higher number of occurrences (in case of multiples char with maximal occurrence count). If instead you want to get the lower, change if (counts[i] >= max_count) for if (counts[i] > max_count).



                Conversely, it print the lower duplicated number with the lower count, to get the higher duplicated with the lower count, change counts[i] < min_count with counts[i] <= min_count.






                share|improve this answer















                • First, you should care about indentation.

                • Why slitting the numbers string while you can access individual char with numbers.charAt(n) ? It force you to work with String instead of char.

                • You don't check for validity of the string. Here, it's hard coded, but if you have to get it from a unknown source, a good habit is to validate it before using.

                • Also, you don't check if there's duplicates or not. If numbers is "1234567890" your program go for a infinite loop.

                • You compute the "Max" count into the main, and the "Min" in a function; try to be consistent.

                • For both computations, you make $n*n$ iterations (where $n$ is the numbers length) giving a complexity of $ O(n^2)$ for both.


                Instead, try to construct a table of occurrences by traversing the table once.
                After, simply search in this table the min and max values in one traversal.



                There's a very naive implementation, but i think a lot simpler to understand than your, and surely more efficient.



                public class X {
                public static void main(String args) {

                String data = "756655874075297346";
                int counts = new int[10];

                for (int i = 0; i < data.length(); i++) {
                char n = data.charAt(i);
                if (n >= '0' && n <= '9') {
                counts[n-'0']++;
                }
                }

                int min_index = 0;
                int max_index = 0;
                int min_count = Integer.MAX_VALUE;
                int max_count = 0;

                for (int i = 0; i < 10; i++) {
                if (counts[i] >= max_count) {
                max_index = i;
                max_count = counts[i];
                }
                if (counts[i] > 1 && counts[i] < min_count) {
                min_index = i;
                min_count = counts[i];
                }
                }
                System.out.println("Frequent number is: " + (char)(max_index + '0') + " It appeared " + max_count + " times");
                if (min_count < Integer.MAX_VALUE) {
                System.out.println("Min Frequent number is: " + (char)(min_index + '0') + " It appeared " + min_count + " times");
                }
                else {
                System.out.println("There's no duplicates!");
                }
                }
                }


                Here, the function print the higher number with the higher number of occurrences (in case of multiples char with maximal occurrence count). If instead you want to get the lower, change if (counts[i] >= max_count) for if (counts[i] > max_count).



                Conversely, it print the lower duplicated number with the lower count, to get the higher duplicated with the lower count, change counts[i] < min_count with counts[i] <= min_count.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 days ago

























                answered 2 days ago









                Calak

                1,941314




                1,941314






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208352%2fget-the-most-frequent-number-and-least-frequent-duplicate-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

                    Quarter-circle Tiles

                    build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                    Mont Emei