https://school.programmers.co.kr/learn/courses/30/lessons/120812
class Solution {
fun solution(array: IntArray): Int {
val countArray = IntArray(1000) { 0 }
array.forEach {
countArray[it] = countArray.getOrElse(it) { 0 } + 1
}
val max = countArray.sorted().last()
val maxFirstIndex = countArray.indexOfFirst { it == max }
val maxLastIndex = countArray.indexOfLast { it == max }
return if (maxFirstIndex == maxLastIndex) maxFirstIndex else -1
}
}
일단 원시적으로 풀었는데, 다음에 더 나은 접근을 시도해보자.
class Solution {
fun solution(n: Int): IntArray = IntArray(n) { it + 1 }
.filter { it % 2 == 1 }
.toIntArray()
}
이렇게 풀었는데, (0..n)
이렇게 도 0~n 까지 array를 만들 수 있다.