최빈값을 구하는 알고리즘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.Scanner; class Solution{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); String input1 = sc.nextLine(); int tc = Integer.parseInt(input1); Solution sol = new Solution(); for(int inx = 0 ; inx < tc; inx++){ String input2 = sc.nextLine(); String input_score = sc.nextLine(); String[] student_score = input_score.split(" "); String result = sol.getModeValue(student_score); System.out.println("#"+ input2+ " "+result); } } public String getModeValue(String[] input){ int[] array = new int[1000]; for(int inx = 0 ; inx < input.length ; inx++){ int num = Integer.parseInt(input[inx]); array[num] = array[num]+1; } int modeValue = 0; int count = 0; for(int inx = 0 ; inx < array.length ; inx++){ if(array[inx] >= count && inx > modeValue){ modeValue = inx; count = array[inx]; } } return String.valueOf(modeValue); } } |
출처 : https://www.swexpertacademy.com/main/learn/course/lectureProblemViewer.do#none