728x90
나머지 구하기
class Solution {
fun solution(num1: Int, num2: Int) = num1%num2
}
중앙값 구하기
class Solution {
fun solution(array: IntArray) = array.sorted()[array.size/2]
}
최빈값 구하기
class Solution {
fun solution(array: IntArray): Int {
var count: Int = 0
var answer: Int = 0
var bool: Int = 0
var arr = Array(2000){0}
for(i:Int in 0..array.size-1) {
arr[array[i]+1000]++
if (arr[array[i]+1000] == count)
bool++
else if (arr[array[i]+1000] > count) {
answer = array[i]
count++
bool = 0
}
}
if (bool > 0)
answer = -1
return answer
}
}
짝수는 싫어요
class Solution {
fun solution(n: Int) = (1..n step(2)).filter{it % 2 == 1}.toIntArray()
}
728x90