This article explores different ways to convert a set to an array in Kotlin.

1. Using toTypedArray() function

The Set interface has the toTypedArray() function, which returns a Typed Array containing the set elements.

Download Code

 
To return a primitive int array from Set of Integer, you can call the toIntArray() function.

Download Code

2. Using Java 8 Stream

Another solution is to use Stream to convert the Set into an array. The idea is to use the toArray() function, which returns an array containing the Stream elements.

Download Code

3. Using for loop

Alternatively, you can iterate over the given set and assign each encountered element to the array one by one.

Download Code

That’s all about converting a set to an array in Kotlin.