Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static String findClass(String[] array) {
- Set<String> h = new HashSet<>(Arrays.asList(array));
- String[] unique = h.toArray(new String[0]);
- int[] counts = new int[unique.length];
- for(int i = 0; i < unique.length; i++) {
- for(int j = 0; j < array.length; j++) {
- if(array[j].equals(unique[i])) {
- counts[i]++;
- }
- }
- }
- int max = counts[0];
- for(int counter = 1; counter < counts.length; counter++) {
- if(counts[counter] > max) {
- max = counts[counter];
- }
- }
- int freq = 0;
- for(int counter = 0; counter < counts.length; counter++) {
- if(counts[counter] == max) {
- freq++;
- }
- }
- int index = -1;
- if(freq == 1) {
- for(int counter = 0; counter < counts.length; counter++) {
- if(counts[counter] == max) {
- index = counter;
- break;
- }
- }
- return unique[index];
- } else {
- int[] ix = new int[freq];
- int ixi = 0;
- for(int counter = 0; counter < counts.length; counter++) {
- if(counts[counter] == max) {
- ix[ixi] = counter;
- ixi++;
- }
- }
- Random rnd = new Random();
- int r = rnd.nextInt(ix.length);
- int n = ix[r];
- return unique[n];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement