Alternate Elements Of Array (Please Help)

Viewed 29

How to print alternate elements of an array in Kotlin. for example elements of an array are (1,2,3,4,5,6,7,8,9) then the output should be (1,3,5,7,9)

1 Answers

listOf(1,2,3,4,5,6,7,8,9).filterIndexed { idx,_ -> idx % 2 == 0 }

will do the job. You might also want to have a look at the fold method on lists that will help you carry out more complex tasks.

Related