This will do the trick without slicing or excessiv...
# codereview
y
This will do the trick without slicing or excessive manipulations with collections
Copy code
fun <Comparable> Array<Comparable>.indexOf(needle: Array<Comparable>): Int {
    if (needle.size > this.size) {
        return -1
    }

    this.forEachIndexed { pos, elem ->
        if (elem == needle[0] && 
          	pos + needle.size - 1 < this.size) {
            var found = true
            for (i in needle.indices) {
                if (this[pos+i] != needle[i]) {
                    found = false
                    break
                }
            }

            if (found) {
                return indexOf@pos
            }
        }
    }

    return -1
}

fun main(args: Array<String>) {
	val haystack = arrayOf(1,2,3,5,6,7,8)
	val needle = arrayOf(5,6)
    
    val stringHaystack = arrayOf("a", "b", "c", "d", "e", "f", "g")
    val stringNeedle = arrayOf("e", "f")
    
    println("IntHaystack needle at pos ${haystack.indexOf(needle)}")
    println("StringHaystack needle at pos ${stringHaystack.indexOf(stringNeedle)}")
}