본문 바로가기
코딩테스트 준비(kotlin)/기본

(kotlin) 나누어 떨어지는 숫자 배열

by 1chanhue1 2024. 4. 18.

 

package com.example.codingtest_practice1.programmers.basicproblems


fun solution(arr: IntArray, divisor: Int): IntArray {
    var answer = intArrayOf()
    var mutableList = mutableListOf<Int>()

    var arrindex = 0

    while (arr.size > arrindex) {
        if (arr[arrindex] % divisor == 0) {
            mutableList.add(arr[arrindex])
        }
        arrindex++


    }
    if (mutableList.isEmpty()) {
        mutableList.add(-1)
    }
    mutableList.sort()
    answer = mutableList.toIntArray()



    return answer
}