Skip to content

Commit

Permalink
Update set-operations.md
Browse files Browse the repository at this point in the history
Moved code comments to a new line, added colon before code blocks, and removed '()' from List.
  • Loading branch information
AlejandraPedroza authored Oct 27, 2023
1 parent 186aad1 commit 1f25528
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions docs/topics/set-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ fun main() {

println(numbers intersect setOf("two", "one"))
println(numbers subtract setOf("three", "four"))
println(numbers subtract setOf("four", "three")) // same output
println(numbers subtract setOf("four", "three"))
// same output
//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()`.
For this operation (known as symmetric difference), calculate the differences between the two collections and merge the
results.
results:

```kotlin
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))
// merge differences
//sampleEnd
}
```
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}

You can also apply `union()`, `intersect()`, and `subtract()` to `List()`.
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.

Expand All @@ -52,8 +54,10 @@ 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) // result on two lists is a Set
println(list1 union list2) // equal elements are merged into one
println(list1 intersect list2)
// result on two lists is a Set
println(list1 union list2)
// equal elements are merged into one
//sampleEnd
}
```
Expand Down

0 comments on commit 1f25528

Please sign in to comment.