Dear users,
How do you sort an array of duplicate elements by the inverse number of occurrences of the elements?
The following is my way of doing it, but I am not sure if it is logically optimised. Could you give me some feedback?
Thanks in advance!
(
~occurrences = { |array|
array.asSet.asArray
.collect { |uniqueItem, uniqueIndex|
Dictionary[\uniqueItem->uniqueItem, \occurrence->array.occurrencesOf(uniqueItem)]
}.sortBy(\occurrence)
.reverse.collect { |item| item.atAll([\uniqueItem, \occurrence]) }
.do {|item| item.postcs }
}
)
~occurrences.([1, 2, 2, 33, 33, 33])
~array = [1, 300, 6, 213, 21, 98, 12, 8, 3, 164, 258, 338, 3, 23, 21, 161, 62, 168, 42, 359, 65, 207, 19, 8, 21, 358, 85, 336, 250, 65, 188, 54, 12, 78, 26, 88, 12, 299, 14, 24, 3, 29, 6, 3, 42, 117, 358, 217, 47, 162, 169, 119, 228, 367, 117, 170, 8, 320, 130, 168, 3, 6, 74, 361, 44, 244, 115, 6]
x = ~occurrences.(~array)