Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Kotlin Dictionary
  3. Set — setOf() / mutableSetOf()

Set — setOf() / mutableSetOf()

In Kotlin, use setOf() to create an immutable set and mutableSetOf() to create a mutable set. A set is a collection with no duplicate elements, and supports set operations such as union(), intersect(), and subtract().

Syntax

// Create an immutable set (duplicates are removed automatically)
val fruits = setOf("apple", "banana", "apple", "orange")
// {apple, banana, orange} (1 duplicate removed)

// Create a mutable set
val tags = mutableSetOf("kotlin", "jvm")
tags.add("android")
tags.remove("jvm")

// Set operations
val a = setOf(1, 2, 3, 4)
val b = setOf(3, 4, 5, 6)
val union = a union b          // {1, 2, 3, 4, 5, 6}
val intersect = a intersect b  // {3, 4}
val subtract = a subtract b   // {1, 2}

Method List

MethodDescription
setOf(elements...)Creates an immutable set (duplicates are removed automatically).
mutableSetOf(elements...)Creates a mutable set.
LinkedHashSet<T>()Creates a set that preserves insertion order.
sortedSetOf(elements...)Creates a set sorted in natural order.
set.contains(element)Checks whether the element is in the set (the in operator can also be used).
set.union(other)Returns the union (A ∪ B).
set.intersect(other)Returns the intersection (A ∩ B).
set.subtract(other)Returns the difference (A − B).
MutableSet.add(element)Adds an element (duplicates are ignored).
MutableSet.remove(element)Removes an element.
list.toSet()Converts a list to a set (useful for removing duplicates).
list.distinct()Returns a list with duplicates removed (preserves order).

Sample Code

fun main() {
    // Basic set (duplicates removed automatically)
    val fruits = setOf("apple", "banana", "apple", "orange", "banana")
    println("fruits: $fruits")  // Duplicates removed
    println("Size: ${fruits.size}")

    // Membership check
    println("banana exists: ${"banana" in fruits}")  // true
    println("melon exists: ${"melon" in fruits}")    // false

    println()

    // Mutable set
    val langs = mutableSetOf("Kotlin", "Java", "Python")
    langs.add("Swift")
    langs.add("Kotlin")   // Duplicate — ignored.
    langs.remove("Java")
    println("Languages: $langs")

    println()

    // Set operations
    val set1 = setOf(1, 2, 3, 4, 5)
    val set2 = setOf(3, 4, 5, 6, 7)

    println("Union: ${set1 union set2}")           // {1,2,3,4,5,6,7}
    println("Intersection: ${set1 intersect set2}") // {3,4,5}
    println("Difference: ${set1 subtract set2}")    // {1,2}
    println("Reverse difference: ${set2 subtract set1}") // {6,7}

    println()

    // Removing duplicates from a list
    val names = listOf("Alice", "Bob", "Alice", "Carol", "Bob", "Dave")
    println("Original list (${names.size} items): $names")

    val unique = names.toSet()
    println("Deduplicated (${unique.size} items): $unique")

    // distinct() — removes duplicates while preserving order.
    val distinctList = names.distinct()
    println("distinct (${distinctList.size} items): $distinctList")

    println()

    // Practical example: finding mutual friends.
    val aliceFriends = setOf("Bob", "Carol", "Dave", "Eve")
    val bobFriends = setOf("Alice", "Carol", "Frank", "Eve")
    val common = aliceFriends intersect bobFriends
    println("Mutual friends: $common")
}

Notes

A set is a collection designed for fast membership checks with no duplicates. Checking whether an element exists with contains() is O(n) for a list but O(1) for a set (HashSet). Sets are a good choice when you frequently need to check whether a large dataset contains a specific element.

Kotlin's setOf() uses LinkedHashSet by default, which preserves insertion order. If you do not need insertion order and want a pure HashSet, use hashSetOf() instead.

For map operations, see Maps — mapOf() / mutableMapOf(). For list basics, see Lists — listOf() / mutableListOf().

If you find any errors or copyright issues, please .