• + 0 comments

    Flag array method (write in kotlin):

    fun pickingNumbers(a: Array<Int>): Int {
        // Write your code here
        var maxLength = 1
        var maxValue = 0
        val frequencies = MutableList(100) { 0 }
    
        a.forEach { num ->
            maxValue = Math.max(num, maxValue)
            frequencies[num]++
        }
    
        for (i in 1..maxValue) {
            val subListLength = frequencies[i] + frequencies[i - 1]
            maxLength = Math.max(maxLength, subListLength)
        }
    
        return maxLength
    }``