Averages/Mode: Difference between revisions

Line 2,422:
Mode of vec2 is: [1,4] // may also print [4, 1], vector has no order guarantee
</pre>
 
 
=={{header|S-lang}}==
I'm accepting strings and numbers, although I'm converting numbers to strings,
as S-Lang Assoc_Type only accepts strings as keys.
<lang S-lang>variable modedat = Assoc_Type[Int_Type, 0];
variable indat = {"foo", "2.3", "bar", "foo", "foobar", "quality", 2.3, "strnen"};
 
foreach $1 (indat)
modedat[string($1)]++;
 
variable mx = 0, mxkey = {};
define find_max(key) {
if (modedat[key] > mx) {
mx = modedat[key];
mxkey = {key};
}
else if (modedat[key] == mx) {
list_append(mxkey, key);
}
}
 
array_map(Void_Type, &find_max, assoc_get_keys(modedat));
if (length(mxkey) > 1) {
$2 = 0;
foreach $1 (mxkey) {
() = printf("%s%s", $2 ? ", " : "", $1);
$2 = 1;
}
() = printf(" each have ");
}
else
() = printf("%s has ", mxkey[0], mx);
() = printf("the most entries at %d\n", mx);</lang>
 
{{out}}
<pre>foo, 2.3 each have the most entries at 2</pre>
 
=={{header|Scala}}==
Anonymous user