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)
Asked
Active
Viewed 113 times
1 Answers
1
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.

aduchate
- 1,455
- 1
- 10
- 5
-
Thank You for the idea. I think at last (.forEach{println(it)}) this should come -----> listOf(1,2,3,4,5,6,7,8,9).filterIndexed{ idx,_ -> idx % 2 == 0 }.forEach{println(it)} } – Saneer Sha Sep 17 '22 at 17:21