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");
}
}
java performance array statistics
add a comment |
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");
}
}
java performance array statistics
add a comment |
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");
}
}
java performance array statistics
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
java performance array statistics
edited 2 days ago
200_success
127k15148411
127k15148411
asked 2 days ago
king amada
296
296
add a comment |
add a comment |
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 withnumbers.charAt(n)
? It force you to work withString
instead ofchar
. - 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
.
add a comment |
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 withnumbers.charAt(n)
? It force you to work withString
instead ofchar
. - 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
.
add a comment |
up vote
1
down vote
accepted
- First, you should care about indentation.
- Why slitting the
numbers
string while you can access individual char withnumbers.charAt(n)
? It force you to work withString
instead ofchar
. - 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
.
add a comment |
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 withnumbers.charAt(n)
? It force you to work withString
instead ofchar
. - 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
.
- First, you should care about indentation.
- Why slitting the
numbers
string while you can access individual char withnumbers.charAt(n)
? It force you to work withString
instead ofchar
. - 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
.
edited 2 days ago
answered 2 days ago
Calak
1,941314
1,941314
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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