8
$\begingroup$

If I have a list

A = {51, 2, 3, 4, 5, 6, 7, 52, 55};

How do I get the highest three numbers of A to get

{51, 52, 55}

I can think of ranking them first, but is there a faster way to do this?

$\endgroup$
2
  • $\begingroup$ Does the order in the result matter? $\endgroup$ Commented Feb 11, 2020 at 11:26
  • 3
    $\begingroup$ Maybe Sort[A][[-3;;]]? $\endgroup$
    – user1066
    Commented Feb 11, 2020 at 16:09

2 Answers 2

12
$\begingroup$

If the order does not matter, TakeLargest[A, 3]; if it does, A[[Ordering[A, -3]]].

$\endgroup$
3
$\begingroup$

Another way

MaximalBy[A, Identity, 3]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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