Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 30, 2017 Finding the index of the maximum value in an associative array | ||||
---|---|---|---|---|
| ||||
Hi, I have an associative array of type int[ulong] and I'm trying to get the index of the maximum value, like this: int[ulong] aa = [1UL: 2000, 2UL: 5000, 5UL: 1000]; writeln(aa.maxIndex); // should print 2 Can anyone help me out? Many thanks, Lyle |
May 30, 2017 Re: Finding the index of the maximum value in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lyle | On Tuesday, 30 May 2017 at 17:57:04 UTC, Lyle wrote:
> Hi,
>
> I have an associative array of type int[ulong] and I'm trying to get the index of the maximum value, like this:
>
> int[ulong] aa = [1UL: 2000,
> 2UL: 5000,
> 5UL: 1000];
>
> writeln(aa.maxIndex); // should print 2
>
> Can anyone help me out?
>
> Many thanks,
> Lyle
Simple enough: get all key/values pairs in the AA, and ask for the element which is the maximum relatively to the value, then take its key.
auto maxIndex(int[ulong] aa) {
import std.algorithm;
return aa.byKeyValue
.maxElement!(x => x.value)
.key;
}
|
May 30, 2017 Re: Finding the index of the maximum value in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lyle | On Tue, May 30, 2017 at 05:57:04PM +0000, Lyle via Digitalmars-d-learn wrote: > Hi, > > I have an associative array of type int[ulong] and I'm trying to get the index of the maximum value, like this: > > int[ulong] aa = [1UL: 2000, > 2UL: 5000, > 5UL: 1000]; > > writeln(aa.maxIndex); // should print 2 > > Can anyone help me out? [...] Try this: void main() { import std.algorithm.iteration : fold; import std.array : byPair; import std.stdio : writeln; int[ulong] aa = [1UL: 2000, 2UL: 5000, 5UL: 1000]; writeln(aa.byPair .fold!((a,b) => a[1] < b[1] ? b : a)[0]); } Note that an associative array really isn't the best data structure if you'll be performing this operation frequently, because you'll be iterating over the entire contents each time. You may want to consider using a sorted container like a RedBlackTree in std.container or something similar instead. T -- Ignorance is bliss... until you suffer the consequences! |
May 30, 2017 Re: Finding the index of the maximum value in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Tuesday, 30 May 2017 at 18:05:02 UTC, cym13 wrote:
> On Tuesday, 30 May 2017 at 17:57:04 UTC, Lyle wrote:
>> Hi,
>>
>> I have an associative array of type int[ulong] and I'm trying to get the index of the maximum value, like this:
>>
>> int[ulong] aa = [1UL: 2000,
>> 2UL: 5000,
>> 5UL: 1000];
>>
>> writeln(aa.maxIndex); // should print 2
>>
>> Can anyone help me out?
>>
>> Many thanks,
>> Lyle
>
> Simple enough: get all key/values pairs in the AA, and ask for the element which is the maximum relatively to the value, then take its key.
>
> auto maxIndex(int[ulong] aa) {
> import std.algorithm;
>
> return aa.byKeyValue
> .maxElement!(x => x.value)
> .key;
> }
Thank you! I used this solution in the end. I had looked at the .byKeyValue and .byPair methods but they didn't seem to work - turns out I was using an old DMD version!
|
June 01, 2017 Re: Finding the index of the maximum value in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Tuesday, 30 May 2017 at 18:05:07 UTC, H. S. Teoh wrote: > On Tue, May 30, 2017 at 05:57:04PM +0000, Lyle via Digitalmars-d-learn wrote: >> Hi, >> >> I have an associative array of type int[ulong] and I'm trying to get the index of the maximum value, like this: >> >> int[ulong] aa = [1UL: 2000, >> 2UL: 5000, >> 5UL: 1000]; >> >> writeln(aa.maxIndex); // should print 2 >> >> Can anyone help me out? > [...] > > Try this: > > void main() { > import std.algorithm.iteration : fold; > import std.array : byPair; > import std.stdio : writeln; > > int[ulong] aa = [1UL: 2000, > 2UL: 5000, > 5UL: 1000]; > > writeln(aa.byPair > .fold!((a,b) => a[1] < b[1] ? b : a)[0]); > } writeln(aa.byPair.maxElement!(p => p[1])[0]); or with https://github.com/dlang/phobos/pull/5436 : writeln(aa.byPair.maxElement!(p => p.value).key); |
Copyright © 1999-2021 by the D Language Foundation