Junior — Middle
Can you explain the concept of multiset and how it differs from a regular set?
sobes.tech AI
Answer from AI
A multiset (multi-set) is a collection of elements where repetitions are allowed. Unlike a regular set, where each element can appear only once, in a multiset the same element can be stored multiple times.
For example, in C++ STL, there is a container called std::multiset, which stores elements in sorted order and allows duplicates:
#include <iostream>
#include <set>
int main() {
std::multiset<int> ms = {1, 2, 2, 3, 3, 3};
for (int x : ms) {
std::cout << x << " ";
}
// Output: 1 2 2 3 3 3
}
Thus, the main difference between a multiset and a set is the ability to store multiple identical elements.