Multiset Implementation in Kotlin
A
MultiSet is a collection that can accommodate duplicate elements, unlike a Set. It does this by maintaining the total number of occurrences of each element in the collection.
Kotlin doesn’t provide MultiSet implementation, but we can write our own implementation of the MultiSet data structure. The following is a simple implementation of MultiSet in Kotlin that uses two Lists – one to store the distinct elements and another to store their counts.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
/* A class representing a Mutable MultiSet */ internal class MultiSet<E> { /* List to store distinct values */ private var values: MutableList<E> = ArrayList() /* List to store counts of distinct values */ private var frequency: MutableList<Int> = ArrayList() /* Error message */ private val ERROR_MSG = "Count cannot be negative: " /** * Adds an element to this multiset specified number of times * * @param `element` The element to be added * @param `count` The number of times * @return The previous count of the element */ fun add(element: E, count: Int): Int { require(count >= 0) { ERROR_MSG + count } val index = values.indexOf(element) var prevCount = 0 if (index != -1) { prevCount = frequency[index] frequency[index] = prevCount + count } else if (count != 0) { values.add(element) frequency.add(count) } return prevCount } /** * Adds specified element to this multiset * * @param `element` The element to be added * @return true always */ fun add(element: E): Boolean { return add(element, 1) >= 0 } /** * Adds all elements in the specified collection to this multiset * * @param `c` Collection containing elements to be added * @return true if all elements are added to this multiset */ fun addAll(c: Collection<E>): Boolean { for (element in c) { add(element, 1) } return true } /** * Adds all elements in the specified array to this multiset * * @param `arr` An array containing elements to be added */ fun addAll(vararg arr: E) { for (element in arr) { add(element, 1) } } /** * Performs the given action for each element of the Iterable, * including duplicates * * @param `action` The action to be performed for each element */ fun forEach(action: (E) -> Unit) { val all: MutableList<E> = ArrayList() for (i in values.indices) { for (j in 0 until frequency[i]) { all.add(values[i]) } } all.forEach(action) } /** * Removes a single occurrence of the specified element from this multiset * * @param `element` The element to removed * @return true if an occurrence was found and removed */ fun remove(element: Any?): Boolean { return remove(element, 1) > 0 } /** * Removes a specified number of occurrences of the specified element * from this multiset * * @param `element` The element to remove * @param `count` The number of occurrences to be removed * @return The previous count */ fun remove(element: Any?, count: Int): Int { require(count >= 0) { ERROR_MSG + count } val index = values.indexOf(element) if (index == -1) { return 0 } val prevCount = frequency[index] if (prevCount > count) { frequency[index] = prevCount - count } else { values.removeAt(index) frequency.removeAt(index) } return prevCount } /** * Check if this multiset contains at least one occurrence of the * specified element * * @param `element` The element to be checked * @return true if this multiset contains at least one occurrence * of the element */ operator fun contains(element: Any?): Boolean { return values.contains(element) } /** * Update the frequency of an element to the specified count or * add the element to this multiset if not present * * @param `element` The element to be updated * @param `count` The new count * @return The previous count */ fun setCount(element: E, count: Int): Int { require(count >= 0) { ERROR_MSG + count } if (count == 0) { remove(element) } val index = values.indexOf(element) if (index == -1) { return add(element, count) } val prevCount = frequency[index] frequency[index] = count return prevCount } /** * Find the frequency of an element in this multiset * * @param `element` The element to be counted * @return The frequency of the element */ fun count(element: Any?): Int { val index = values.indexOf(element) return if (index == -1) 0 else frequency[index] } /** * @return A view of the set of distinct elements in this multiset */ fun elementSet(): Set<E> { return values.toSet() } /** * @return true if this multiset is empty */ val isEmpty: Boolean get() = values.size == 0 /** * @return Total number of elements in this multiset, including duplicates */ fun size(): Int { var size = 0 for (i in frequency) { size += i } return size } /** * @return String representation of this multiset */ override fun toString(): String { val sb = StringBuilder("[") for (i in values.indices) { sb.append(values[i]) if (frequency[i] > 1) { sb.append(" x ").append(frequency[i]) if (i != values.size - 1) { sb.append(", ") } } } return sb.append("]").toString() } } fun main() { val multiset: MultiSet<String?> = MultiSet() multiset.add("USA") multiset.add("Japan", 2) multiset.addAll("India", "China") multiset.addAll(listOf("USA", "India", "China", "Japan")) println(multiset) multiset.remove("China") multiset.remove("Japan", 2) println(multiset) multiset.setCount("USA", 4) multiset.setCount("Japan", 5) multiset.setCount("Mexico", 3) multiset.setCount("China", 0) println(multiset) } |
Output:
[USA x 2, Japan x 3, India x 2, China x 2]
[USA x 2, Japan, India x 2, China]
[USA x 4, Japan x 5, India x 2, Mexico x 3]
That’s all about Multiset implementation in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)