Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Overview about the Lesson
Operators are special symbols that are meant to perform special calculations, comparisons, and logical manipulations. And the variables on which these operations are performed are called Operands. For example, in x + y; x and y are operands and + (plus) is the operator.




The Content Of The Lesson
- Introduction
- Grouping and counting the characters in strings and collections
- Set operations in Kotlin
- Examples
Why This Lesson
This lesson is important because it demonstrates how to use set operations in Kotlin to solve complex problems. By understanding the set operations in Kotlin, you will be able to write more efficient and effective code.
Set Operations and Grouping in Kotlin: Worked Examples
This lesson’s PDF shows how idiomatic Kotlin solves puzzles with set operations and grouping. Here are the key functions with small, runnable examples.
Set operations: union, intersect, subtract
fun main() {
val morning = setOf("Ali", "Mona", "Sara")
val evening = setOf("Sara", "Omar", "Mona")
println(morning union evening) // [Ali, Mona, Sara, Omar]
println(morning intersect evening) // [Mona, Sara]
println(morning subtract evening) // [Ali]
}| Function | Returns | Typical use |
|---|---|---|
| union | Every element in either collection, no duplicates | Combine lists of tags or users |
| intersect | Elements present in both | Find common items, shared characters |
| subtract | Elements in the first but not the second | Find what is missing or new |
These are infix functions, so a union b reads like plain English. They work on any collection and always return a Set.
Finding characters common to several strings
A classic puzzle: find the characters that appear in every word.
fun main() {
val lines = listOf("kotlin", "intro", "tiny")
val common = lines
.map { it.toSet() }
.reduce { acc, chars -> acc intersect chars }
println(common) // [t, i, n]
}Converting each string with toSet() turns it into a set of unique characters, and reduce intersects them one after another.
Grouping and counting
fun main() {
val word = "banana"
println(word.groupingBy { it }.eachCount()) // {b=1, a=3, n=2}
val words = listOf("apple", "avocado", "banana", "blueberry", "cherry")
println(words.groupBy { it.first() })
// {a=[apple, avocado], b=[banana, blueberry], c=[cherry]}
}groupByreturns a map from each key to the list of matching elements.groupingBy { }.eachCount()counts elements per key without building intermediate lists.
Other useful collection helpers
| Function | Example | Result |
|---|---|---|
| distinct | listOf(1,2,2,3).distinct() | [1, 2, 3] |
| chunked | (1..6).toList().chunked(2) | [[1, 2], [3, 4], [5, 6]] |
| windowed | listOf(1,2,3,4).windowed(2) | [[1, 2], [2, 3], [3, 4]] |
| partition | (1..6).partition { it % 2 == 0 } | ([2, 4, 6], [1, 3, 5]) |
| sumOf | listOf("a","bb").sumOf { it.length } | 3 |
Practice exercise
Given two lists of course IDs, print the courses taken by both students and the ones only the first student took.
fun main() {
val first = listOf(101, 102, 103, 104)
val second = listOf(103, 104, 105)
println(first intersect second) // [103, 104]
println(first subtract second) // [101, 102]
}Frequently Asked Questions
Do set operations keep the original order?
The result is a LinkedHashSet, so elements keep the order in which they first appear in the receiver (the left-hand collection) and, for union, then in the second collection.
What is Advent of Code?
An annual series of programming puzzles published every December. It is a popular way to practise idiomatic Kotlin, and JetBrains publishes Kotlin solutions each year.
Continue the course
← Previous: Lesson 5: Operators | Next: Lesson 7: Booleans & If…Else →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping (you are here)
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In conclusion, the blog post “Idiomatic Kotlin: Solving Advent of Code Puzzles, Set Operations” provides a detailed explanation of set operations in Kotlin and how to use them to solve Advent of Code puzzles. By understanding the set operations in Kotlin, you will be able to write more efficient and effective code.
Download From Link
You can download the lesson from the following link:














