1
\$\begingroup\$

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");
}
}
\$\endgroup\$
1
\$\begingroup\$
  • 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.