‫Welcome back.
‫In this video we're going to learn about mutable sets.
‫A mutable set is a collection that allows you to modify its elements after it's created.
‫Mutable sets are used to store a collection of unique elements, and you can add, remove, and modify
‫elements within a mutable set.
‫These sets are part of Kotlin's collection framework and are created using functions like mutable set
‫of.
‫The mutable set of function is used to create a mutable set.
‫In Kotlin.
‫For example, val fruits equals to mutable set of apple, banana, and cherry.
‫This function takes a var arg parameter, allowing you to specify the initial elements of the set.
‫The fruits mutable set can be modified.
‫You can add remove, or update elements in this set.
‫For example, you can modify a mutable set using various functions and operations, and I'll start with
‫the add fruits dot add function and I'll add melon.
‫Melon will add it at the end of this mutable set.
‫Mutable sets provide a flexibility when you need to work with a dynamic collection of unique elements,
‫and as you change the set's content as your program runs.
‫For example, if I try to add an existing element here, I'll add Apple and try to print all the elements
‫of the fruits in the same way.
‫I'll iterate over a set element here and I'll change the set fruits.
‫Let me run and this is the fruits set apple, banana, cherry and melon.
‫You can notice that despite we added the apple element to our fruits set, this apple is duplicate so
‫it will prevent duplicate elements in the set.
‫It ensures that only unique elements are in this set, so this will be omitted or skipped.
‫This is the usage of the sets in Kotlin only unique elements.
‫Here I try to add a duplicate element, an element that exists so it will prevent this addition.
‫To remove elements from a set, we start with the set dot remove and you can add remove if remove function,
‫remove all and so on I remove melon.
‫Also you can update the sets element.
‫For example, if I need to update banana, I need to remove banana and then add orange.
‫In Kotlin, sets are collections of unique elements and by design they don't allow duplicate elements.
‫Since each element is in the set is unique, you can't directly update an element as you might in a
‫mutable list or a mutable map.
‫Instead, to update a set, you typically follow these steps.
‫You remove the old element and add the new element.
‫You can't directly specify the banana and replace it with orange.
‫You need to remove it with the remove function and add it with the add function that adds a new element.
‫Sets automatically ensure the uniqueness of their elements.
‫If you want to update an element based on a specific condition, you can use a loop or an iterator to
‫traverse the set.
‫Find the element you want to update, and then apply the update steps as needed.