How to create a Set of size N in Kotlin


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,

  1. We start by importing the necessary Kotlin package using import kotlin.sequences.generateSequence.
  2. We define a function named createSetOfSizeN that takes an integer n as a parameter. This parameter represents the desired size of the set.
  3. Inside the function, we use the generateSequence function to generate a sequence of numbers starting from 1. The take method is used to limit the sequence to n elements.
  4. We convert the sequence to a set using the toSet method. This ensures that the resulting collection is a set with exactly n unique elements.
  5. The function returns the created set.
  6. In the main function, we call createSetOfSizeN with a specific size (e.g., 5) and store the result in a variable named mySet.
  7. 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,

  1. We start by importing the necessary Kotlin package using import kotlin.random.Random.
  2. We define a function named createRandomSetOfSizeN that takes an integer n as a parameter. This parameter represents the desired size of the set.
  3. Inside the function, we create an empty mutable set named resultSet to store the unique random numbers.
  4. We use a while loop to repeatedly add random numbers to the set until its size equals n. The Random.nextInt function generates random integers.
  5. Once the set reaches the desired size, the function returns the created set.
  6. In the main function, we call createRandomSetOfSizeN with a specific size (e.g., 5) and store the result in a variable named myRandomSet.
  7. 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

  1. How to create an Empty Set in Kotlin ?
  2. How to Initialize a Set in Kotlin ?
  3. How to Get Length of a Set in Kotlin ?
  4. How to create a Set of size N in Kotlin ?
  5. How to create a Set of Numbers from 1 to N in Kotlin ?
  6. How to create a Set of integers in Kotlin ?
  7. How to create a Set of Strings in Kotlin ?
  8. How to Access Items in a Set in Kotlin ?
  9. How to get a Random Item in a Set in Kotlin ?
  10. How to Iterate Over a Set in Kotlin ?
  11. How to check if a Set is Empty in Kotlin ?
  12. How to check if a Set is Not Empty in Kotlin ?
  13. How to get Subset from a Set in Kotlin ?
  14. How to check if a Specific Item is present in the Set in Kotlin ?
  15. How to check if a Set contains all the items of Another Set in Kotlin ?
  16. How to Sort Items of a Set in Kotlin ?
  17. How to Copy a Set in Kotlin ?
  18. How to add an Item to a Set in Kotlin ?
  19. How to find Union of Two Sets in Kotlin ?
  20. How to find Intersection of Two Sets in Kotlin ?
  21. How to check if Two Sets are Equal in Kotlin ?
  22. How to Convert a Set of Integers to a Set of Strings in Kotlin ?
  23. How to Convert a Set of Strings to a Set of Integers in Kotlin ?
  24. How to Convert a Set of Floats to a Set of Strings in Kotlin ?
  25. How to Convert a Set of Strings to a Set of Floats in Kotlin ?
  26. How to Filter Items of a Set based on a Condition in Kotlin ?
  27. How to Remove Items from Set based on a Condition in Kotlin ?
  28. How to create a Set of Sets in Kotlin ?