3
$\begingroup$

I have an association:

1->{10,50,100}, 2->{2,4,6,9,60,90}, 3->{3,7,12,1425}, 4->{8,13,17,22,24,26,29}, 5->{18,23,27,32,97}, 6->{28,33,37,48,98}, 7->{38,78}, 8->{88}

I want to get the value of the largest key i.e. 8, from the above association i.e. 88. Is there a way to accomplish this?

$\endgroup$
  • 1
    $\begingroup$ The function KeySort might also be notable in case you want to perform several of such iindexing by ordering of the keys; then you can order once a = KeySort[a] and access smallest and largest key by a[[1]] and a[[-1]] (and so on...) $\endgroup$ – Henrik Schumacher May 9 at 11:31
6
$\begingroup$
assoc = Association[1 -> {1, 5, 10, 50, 100}, 
  2 -> {2, 4, 6, 9, 11, 15, 20, 40, 51, 55, 60, 90}, 
  3 -> {3, 7, 12, 14, 16, 19, 21, 25, 30, 41, 45, 52, 54, 56, 59, 61, 
    65, 70, 91, 95}, 
  4 -> {8, 13, 17, 22, 24, 26, 29, 31, 35, 42, 44, 46, 49, 53, 57, 62,
     64, 66, 69, 71, 75, 80, 92, 94, 96, 99}, 
  5 -> {18, 23, 27, 32, 34, 36, 39, 43, 47, 58, 63, 67, 72, 74, 76, 
    79, 81, 85, 93, 97}, 
  6 -> {28, 33, 37, 48, 68, 73, 77, 82, 84, 86, 89, 98}, 
  7 -> {38, 78, 83, 87}, 8 -> {88}];


assoc[Max @ Keys @ assoc]
{88}

Also

MaximalBy[First] @ assoc (* thanks: @swish *)
<|8 -> {88}|>
| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ MaximalBy works on associations, no need for Normal. $\endgroup$ – swish May 9 at 10:48
  • $\begingroup$ thank you @swish. Updated with your suggestion. $\endgroup$ – kglr May 9 at 10:52
1
$\begingroup$
Function[x, x@*Max@AssociationMap[Reverse][x]]@assoc

although I would use the method proposed by @kglr i.e.

assoc[Max @ Keys @ assoc]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.