Skip to content

Instantly share code, notes, and snippets.

@MartinIngesen
Last active August 29, 2015 14:08
Show Gist options
  • Save MartinIngesen/f28e11ea14fba5bb87dc to your computer and use it in GitHub Desktop.
Save MartinIngesen/f28e11ea14fba5bb87dc to your computer and use it in GitHub Desktop.
Utfordring 1
public class Main {
public static void main(String[] args) {
int siffer = mfdString(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE + "mfd (String) er: " + siffer);
siffer = mfdInt(Integer.MAX_VALUE);
System.out.println("mfd (int) er: " + siffer);
}
private static int mfdString(int i) {
String number = String.valueOf(i);
int[] array = new int[10];
for (int j = 0; j < number.length(); j++) {
int tall = number.charAt(j) - '0';
array[tall] = array[tall] + 1;
}
int tallet = 0;
for (int j = 0; j < 10; j++) {
if (array[tallet] < array[j]) {
tallet = j;
}
}
return tallet;
}
private static int mfdInt(int i) {
int[] array = new int[10];
while (i > 0) {
int n = i % 10;
i = i / 10;
array[n] = array[n] + 1;
}
int tallet = 0;
for (int j = 0; j < 10; j++) {
if (array[tallet] < array[j]) {
tallet = j;
}
}
return tallet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment