Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact
No Result
View All Result
Connect 4 Techs
No Result
View All Result
Home Kotlin

Kotlin Set Operations & Grouping (Lesson 6 PDF)

Ahmed Elhozayen by Ahmed Elhozayen
October 11, 2026
in Kotlin
0
Kotlin Set Operations & Grouping (Lesson 6 PDF)

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.

Kotlin

You might also like

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

Kotlin Functions & Classes (Lesson 11 PDF)

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

  1. Introduction
  2. Grouping and counting the characters in strings and collections
  3. Set operations in Kotlin
  4. 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]
}
FunctionReturnsTypical use
unionEvery element in either collection, no duplicatesCombine lists of tags or users
intersectElements present in bothFind common items, shared characters
subtractElements in the first but not the secondFind 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]}
}
  • groupBy returns 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

FunctionExampleResult
distinctlistOf(1,2,2,3).distinct()[1, 2, 3]
chunked(1..6).toList().chunked(2)[[1, 2], [3, 4], [5, 6]]
windowedlistOf(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])
sumOflistOf("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 →

  1. Lesson 1: Getting Started
  2. Lesson 2: Syntax & Output
  3. Lesson 3: Comments & Variables
  4. Lesson 4: Data Types
  5. Lesson 5: Operators
  6. Lesson 6: Set Operations & Grouping (you are here)
  7. Lesson 7: Booleans & If…Else
  8. Lesson 8: When, While, Break & Continue
  9. Lesson 9: Arrays
  10. Lesson 10: For Loops & Ranges
  11. Lesson 11: Functions & Classes
  12. Lesson 12: Classes, Objects & Constructors
  13. 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:

From Here:

Tags: AndroidJavaJVMKotlinProgrammingtechnology
Previous Post

Kotlin Operators (Lesson 5 PDF)

Next Post

Kotlin Booleans & Kotlin If … Else (Lesson 7 PDF)

Ahmed Elhozayen

Ahmed Elhozayen

Related Posts

Kotlin Class Functions & Inheritance (Lesson 13 PDF)
Kotlin

Kotlin Class Functions & Inheritance (Lesson 13 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin Classes and Objects & Constructors (Lesson 12 PDF)
Kotlin

Kotlin Classes and Objects & Constructors (Lesson 12 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin Functions & Classes (Lesson 11 PDF)
Kotlin

Kotlin Functions & Classes (Lesson 11 PDF)

by Ahmed Elhozayen
October 11, 2026
Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)
Kotlin

Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)

by Ahmed Elhozayen
October 11, 2026
 Kotlin Arrays (Lesson 9 PDF)
Kotlin

 Kotlin Arrays (Lesson 9 PDF)

by Ahmed Elhozayen
October 11, 2026
Next Post
Kotlin Booleans & Kotlin If … Else (Lesson 7 PDF)

Kotlin Booleans & Kotlin If … Else (Lesson 7 PDF)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Excel Formulas Cheat Sheet

Excel Formulas Cheat Sheet PDF

October 11, 2026
20 Essential Excel Formulas Every Data Analyst Must Know in 2026

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

May 18, 2026

Categories

  • apple
  • Canon
  • Data Analysis
  • Excel
  • Finance
  • ios
  • Kotlin
  • Marketing
  • MBA
  • Samsung
  • Software Tutorials
  • Technology

Don't miss it

20 Essential Excel Formulas Every Data Analyst Must Know in 2026
Data Analysis

20 Essential Excel Formulas Every Data Analyst Must Know in 2026

May 18, 2026
Excel guide for data analytics learners
Excel

Excel for Data Analysis PDF

October 11, 2026
The Ultimate Guide to Financial Modeling PDF
Excel

The Ultimate Guide to Financial Modeling PDF

October 11, 2026
The Ultimate Excel Handbook PDF
Excel

The Ultimate Excel Handbook PDF

October 11, 2026
𝟓𝟎 𝐄𝐱𝐜𝐞𝐥 𝐒𝐡𝐨𝐫𝐭𝐜𝐮𝐭 𝐊𝐞𝐲
Excel

50 Excel Shortcut Key PDF: Streamline Your Workflow

March 21, 2025
Most Useful Excel Keyboard Shortcuts For Windows and Mac OS
Excel

Most Useful Excel Keyboard Shortcuts for Windows and Mac OS PDF

March 21, 2025
Connect4Techs Tech

Practical, free guides on Excel, data analysis, finance, business and programming — written to help you learn faster and work smarter.

Follow on Facebook

Topics

  • Excel
  • Kotlin
  • MBA
  • Finance
  • Data Analysis
  • Apple
  • Technology
  • Software Tutorials

Latest articles

  • 20 Essential Excel Formulas Every Data Analyst Must Know in 2026May 18, 2026
  • Excel for Data Analysis PDFAug 31, 2025
  • The Ultimate Guide to Financial Modeling PDFApr 19, 2025

Company

  • About us
  • Contact
  • Privacy Policy
  • Terms of Use
  • IT Certification PDFs
© 2026 Connect4Techs. All rights reserved. Privacy · Terms · Contact
No Result
View All Result
  • Home
  • Excel
  • Data Analysis
  • Finance
  • MBA
    • Marketing
  • Kotlin
  • Gadgets
    • Apple
    • Samsung
    • Canon
    • Tech News
  • About
  • Contact