Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 811479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:03:08+00:00 2026-05-15T01:03:08+00:00

What is the easiest way to get a key with the highest value from

  • 0

What is the easiest way to get a key with the highest value from a hash in Perl?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T01:03:08+00:00Added an answer on May 15, 2026 at 1:03 am

    While the solution with sort:

    (sort {$hash{$a} <=> $hash{$b}} keys %hash)[0]
    

    found in some of the other answers is quite elegant, it doesn’t perform as nicely as it looks. First off, the sort transforms an O(n) search search operation into an O(n log n) one. Secondly, the sort solution has n log n hash look-ups. Hash look-ups are very good for certain operations, but when working with the entire hash, look-ups will be slower than using each, keys, or values to iterate through the data structure. This is because the iterators do not need to calculate the hashes of keys, nor do they need to repeatedly walk through bins to find the values. And the overhead is not constant, but increasing as the hashes get larger.

    Here are a few faster solutions:

    use strict;
    use warnings;
    
    my %hash = (
        small   => 1,
        medium  => 5,
        largest => 10,
        large   => 8,
        tiny    => 0.1,
    );
    

    Here is a solution using the each iterator (an O(1) operation done n times):

    sub largest_value (\%) {
        my $hash = shift;
        keys %$hash;       # reset the each iterator
    
        my ($large_key, $large_val) = each %$hash;
    
        while (my ($key, $val) = each %$hash) {
            if ($val > $large_val) {
                $large_val = $val;
                $large_key = $key;
            }
        }
        $large_key
    }
    
    print largest_value %hash; # prints 'largest'
    

    Or a faster version that trades memory for speed (it makes a copy of the hash):

    sub largest_value_mem (\%) {
        my $hash   = shift;
        my ($key, @keys) = keys   %$hash;
        my ($big, @vals) = values %$hash;
    
        for (0 .. $#keys) {
            if ($vals[$_] > $big) {
                $big = $vals[$_];
                $key = $keys[$_];
            }
        }
        $key
    }
    
    print largest_value_mem %hash; # prints 'largest'
    

    Here is the performance with various hash sizes:

    10 keys:              Rate largest_with_sort largest_value largest_value_mem
    largest_with_sort 111565/s                --           -8%              -13%
    largest_value     121743/s                9%            --               -5%
    largest_value_mem 127783/s               15%            5%                --
    
    50 keys:             Rate  largest_with_sort largest_value largest_value_mem
    largest_with_sort 24912/s                 --          -37%              -40%
    largest_value     39361/s                58%            --               -6%
    largest_value_mem 41810/s                68%            6%                --
    
    100 keys:            Rate  largest_with_sort largest_value largest_value_mem
    largest_with_sort  9894/s                 --          -50%              -56%
    largest_value     19680/s                99%            --              -12%
    largest_value_mem 22371/s               126%           14%                --
    
    1,000 keys:         Rate   largest_with_sort largest_value largest_value_mem
    largest_with_sort  668/s                  --          -69%              -71%
    largest_value     2183/s                227%            --               -7%
    largest_value_mem 2341/s                250%            7%                --
    
    10,000 keys:        Rate   largest_with_sort largest_value largest_value_mem
    largest_with_sort 46.5/s                  --          -79%              -81%
    largest_value      216/s                365%            --              -11%
    largest_value_mem  242/s                421%           12%                --
    

    As you can see, if memory isn’t much of an issue, the version with internal arrays is fastest, closely followed by the each iterator, and in a distant third… sort

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there was a way to send a referer with a
I wrote a PL/SQL script to set a sequence's value to the maximum value
I have defined my Enums like this. public enum UserType { RESELLER(Reseller), SERVICE_MANAGER(Manager), HOST(Host);
This is probably a simple question, but I really don't know what I'm doing
I'm running into a mental roadblock here and I'm hoping that I'm missing something
I have a row of data as follows: header1 header2 header3 header4 header5 row
I've decided to attempt using the double submitted cookies technique to attempt to prevent
To save time Googling my website in order to see where my pages have
Edit: OK I asked the wrong question here. I'm going to be coding a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.