From: Rick DeNatale Date: 2006-10-13T01:04:00+09:00 Subject: Re: Counting Frequency of Values in an Array (And Sorting by Frequency?) On 10/11/06, x1 wrote: > Is there no method for an array that will tell me the # of occurrences > for an item? > > IE: ["a", "a", "a", "b", "c", "c"].count("a") #producing 3 ? > Smalltalk has a collection class called Bag, which is an unordered collection of objects which keeps track of the number of occurences of each equal element in the collection, so you can do something like: bag <- Bag.new bag.add: 1;add: 2: add: 1; add: 4 bag.occurencesOf: 1 => 2 bag.occurencesOf: 1 => 1 bag.remove: 1 bag.occurrencesOf: 1 => 1 Now Bag is kind of the black sheep of the Smalltalk collection classes. Most Smalltalkers would either never use it or overuse it. The only use I could think of was to implement a histogram. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/