Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1.  
  2. private static String findClass(String[] array) {
  3. Set<String> h = new HashSet<>(Arrays.asList(array));
  4. String[] unique = h.toArray(new String[0]);
  5. int[] counts = new int[unique.length];
  6. for(int i = 0; i < unique.length; i++) {
  7. for(int j = 0; j < array.length; j++) {
  8. if(array[j].equals(unique[i])) {
  9. counts[i]++;
  10. }
  11. }
  12. }
  13.  
  14. int max = counts[0];
  15.  
  16. for(int counter = 1; counter < counts.length; counter++) {
  17. if(counts[counter] > max) {
  18. max = counts[counter];
  19. }
  20. }
  21.  
  22. int freq = 0;
  23.  
  24. for(int counter = 0; counter < counts.length; counter++) {
  25. if(counts[counter] == max) {
  26. freq++;
  27. }
  28. }
  29.  
  30. int index = -1;
  31. if(freq == 1) {
  32.  
  33. for(int counter = 0; counter < counts.length; counter++) {
  34. if(counts[counter] == max) {
  35. index = counter;
  36. break;
  37. }
  38. }
  39.  
  40. return unique[index];
  41. } else {
  42. int[] ix = new int[freq];
  43.  
  44. int ixi = 0;
  45. for(int counter = 0; counter < counts.length; counter++) {
  46. if(counts[counter] == max) {
  47. ix[ixi] = counter;
  48. ixi++;
  49. }
  50. }
  51.  
  52.  
  53. Random rnd = new Random();
  54.  
  55. int r = rnd.nextInt(ix.length);
  56.  
  57. int n = ix[r];
  58.  
  59. return unique[n];
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement