Last active
August 29, 2015 14:08
-
-
Save MartinIngesen/f28e11ea14fba5bb87dc to your computer and use it in GitHub Desktop.
Utfordring 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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