Skip to content

Commit

Permalink
Update Set-specific operations
Browse files Browse the repository at this point in the history
Added code comments, formatted code samples, fixed content, added symmetric difference explanation.
  • Loading branch information
AlejandraPedroza committed Nov 1, 2023
1 parent 645bc97 commit ceab85b
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions docs/topics/set-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,38 @@ fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")

// output according to the order
println(numbers union setOf("four", "five"))
// [one, two, three, four, five]
println(setOf("four", "five") union numbers)
// output according to the order
// [four, five, one, two, three]
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

To find an intersection between two collections (elements present in both of them), use [`intersect()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html).
To find collection elements not present in another collection, use [`subtract()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/subtract.html).
To find an intersection between two collections (elements present in both of them), use the [`intersect()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html) function.
To find collection elements not present in another collection, use the [`subtract()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/subtract.html) function.
Both these functions can be called in the infix form as well, for example, `a intersect b`:

```kotlin
fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")

// same output
println(numbers intersect setOf("two", "one"))
// [one, two]
println(numbers subtract setOf("three", "four"))
// [one, two]
println(numbers subtract setOf("four", "three"))
// same output
// [one, two]
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

To find the elements present in either one of the two collections but not in their intersection, you can also use `union()`.
To find the elements present in either one of the two collections but not in their intersection, you can also use the `union()` function.
For this operation (known as symmetric difference), calculate the differences between the two collections and merge the
results:

Expand All @@ -47,26 +53,31 @@ fun main() {
//sampleStart
val numbers = setOf("one", "two", "three")
val numbers2 = setOf("three", "four")
println((numbers-numbers2) union (numbers2-numbers))

// merge differences
println((numbers - numbers2) union (numbers2 - numbers))
// [one, two, four]
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

You can also apply `union()`, `intersect()`, and `subtract()` to `List`.
However, their result is _always_ a `Set`, even on lists. In this result, all the duplicate elements are merged into one
and the index access is not available:
You can also apply `union()`, `intersect()`, and `subtract()` functions to lists.
However, their result is _always_ a `Set`. In this result, all the duplicate elements are merged into one and the index access is not available:

```kotlin
fun main() {
//sampleStart
val list1 = listOf(1, 1, 2 ,3, 5, 8, -1)
val list2 = listOf(1, 1, 2, 2 ,3, 5)
println(list1 intersect list2)
val list1 = listOf(1, 1, 2, 3, 5, 8, -1)
val list2 = listOf(1, 1, 2, 2, 3, 5)

// result on two lists is a Set
println(list1 union list2)
println(list1 intersect list2)
//[1, 2, 3, 5]

// equal elements are merged into one
println(list1 union list2)
// [1, 2, 3, 5, 8, -1]
//sampleEnd
}
```
Expand Down

0 comments on commit ceab85b

Please sign in to comment.