How to create a Set of size N in Kotlin - Step by Step Examples
How to create a Set of size N in Kotlin ?
Answer
To create a set of size N in Kotlin, you can use the generateSequence
function to generate a sequence and then convert it to a set using the toSet
method.
✐ Examples
1 Create a Set of Size N with Sequential Numbers
In this example,
- We start by importing the necessary Kotlin package using
import kotlin.sequences.generateSequence
. - We define a function named
createSetOfSizeN
that takes an integern
as a parameter. This parameter represents the desired size of the set. - Inside the function, we use the
generateSequence
function to generate a sequence of numbers starting from 1. Thetake
method is used to limit the sequence ton
elements. - We convert the sequence to a set using the
toSet
method. This ensures that the resulting collection is a set with exactlyn
unique elements. - The function returns the created set.
- In the
main
function, we callcreateSetOfSizeN
with a specific size (e.g., 5) and store the result in a variable namedmySet
. - Finally, we print the created set to the console using the
println
function.
Kotlin Program
import kotlin.sequences.generateSequence
fun createSetOfSizeN(n: Int): Set<Int> {
return generateSequence(1) { it + 1 }.take(n).toSet()
}
fun main(args: Array<String>) {
val mySet = createSetOfSizeN(5)
println("Created set of size 5: $mySet")
}
Output
Created set of size 5: [1, 2, 3, 4, 5]
2 Create a Set of Size N with Random Numbers
In this example,
- We start by importing the necessary Kotlin package using
import kotlin.random.Random
. - We define a function named
createRandomSetOfSizeN
that takes an integern
as a parameter. This parameter represents the desired size of the set. - Inside the function, we create an empty mutable set named
resultSet
to store the unique random numbers. - We use a
while
loop to repeatedly add random numbers to the set until its size equalsn
. TheRandom.nextInt
function generates random integers. - Once the set reaches the desired size, the function returns the created set.
- In the
main
function, we callcreateRandomSetOfSizeN
with a specific size (e.g., 5) and store the result in a variable namedmyRandomSet
. - Finally, we print the created set to the console using the
println
function.
Kotlin Program
import kotlin.random.Random
fun createRandomSetOfSizeN(n: Int): Set<Int> {
val resultSet = mutableSetOf<Int>()
while (resultSet.size < n) {
resultSet.add(Random.nextInt(1, 100))
}
return resultSet
}
fun main(args: Array<String>) {
val myRandomSet = createRandomSetOfSizeN(5)
println("Created set of size 5 with random numbers: $myRandomSet")
}
Output
Created set of size 5 with random numbers: [12, 47, 85, 24, 37]
Summary
In this tutorial, we learned How to create a Set of size N in Kotlin language with well detailed examples.
More Kotlin Sets Tutorials
- How to create an Empty Set in Kotlin ?
- How to Initialize a Set in Kotlin ?
- How to Get Length of a Set in Kotlin ?
- How to create a Set of size N in Kotlin ?
- How to create a Set of Numbers from 1 to N in Kotlin ?
- How to create a Set of integers in Kotlin ?
- How to create a Set of Strings in Kotlin ?
- How to Access Items in a Set in Kotlin ?
- How to get a Random Item in a Set in Kotlin ?
- How to Iterate Over a Set in Kotlin ?
- How to check if a Set is Empty in Kotlin ?
- How to check if a Set is Not Empty in Kotlin ?
- How to get Subset from a Set in Kotlin ?
- How to check if a Specific Item is present in the Set in Kotlin ?
- How to check if a Set contains all the items of Another Set in Kotlin ?
- How to Sort Items of a Set in Kotlin ?
- How to Copy a Set in Kotlin ?
- How to add an Item to a Set in Kotlin ?
- How to find Union of Two Sets in Kotlin ?
- How to find Intersection of Two Sets in Kotlin ?
- How to check if Two Sets are Equal in Kotlin ?
- How to Convert a Set of Integers to a Set of Strings in Kotlin ?
- How to Convert a Set of Strings to a Set of Integers in Kotlin ?
- How to Convert a Set of Floats to a Set of Strings in Kotlin ?
- How to Convert a Set of Strings to a Set of Floats in Kotlin ?
- How to Filter Items of a Set based on a Condition in Kotlin ?
- How to Remove Items from Set based on a Condition in Kotlin ?
- How to create a Set of Sets in Kotlin ?