I have a map initialized as
val cache: SortedMap<String, String> = sortedMapOf()
The map is used as a cache and can contain duplicated values with their own unique key. I want to check and count how many duplicated values there are in the cache. Note that the cache can contain millions of entries.
As of now, I check for duplicates in this way
val uniqueValueSet = hashSetOf<String>()
val numDuplicates = cache.filter {!uniqueValueSet.add(it.value)}.count()
However, I feel like this check is memory inefficient, where adding all the distinct values to a set creates an obsolete set with possibly millions of elements.
Is there a better, and more optimized, way of checking duplicates between the values in a map?