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

  • SEARCH
  • Home
  • 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 6367857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:36:09+00:00 2026-05-25T00:36:09+00:00

my question is actually similar to: Extracting the most duplicate value from an array

  • 0

my question is actually similar to: Extracting the most duplicate value from an array in JavaScript (with jQuery)

Solution (the one I found as the best, and slightly changed by me):

var arr     = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1],
result  = {}, 
max     = 0, 
res;

for( var i = 0, total = arr.length; i < total; ++i ) {
var val = arr[i],
    inc = ( result[val] || 0 ) + 1;

result[val] = inc;

if( inc > max ) { 
    max = inc;
    res = val;
}
}
alert(res);

I would like to add an extra which is : if we have, say two numbers with the same number of occurrences, how could we find the minimum of them (the above should alert 5 and not 7, which is the case)? The current solution works for getting only the first most duplicate found, but it does not deal with repetition.
Thanks!

  • 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-25T00:36:10+00:00Added an answer on May 25, 2026 at 12:36 am

    Sort the array before you count the incidences:

    var arr = [3, 7, 7, 7, 7, 10, 10, 8, 5, 5, 5, 5, 20, 20, 1];
    
    function min_most_duplicate (arr) {
        var result = {},
            max = 0,
            res;
    
        arr = arr.slice(0); // make a copy of the array
        arr.sort(function(a, b) { return (a - b); }); // sort it numerically
    
        for( var i = 0, total = arr.length; i < total; ++i ) {
            var val = arr[i],
            inc = ( result[val] || 0 ) + 1;
    
            result[val] = inc;
    
            if( inc > max ) { 
                max = inc;
                res = val;
            }
        }
    
        return res;
    }
    
    min_most_duplicate(arr); // returns 5
    

    This works because the way the for loop is written it’s going to return the first one that it finds that has the most duplicates, so if the array is sorted, the smallest number will come first, so it’ll be the one that the for loop finds.

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

Sidebar

Related Questions

Similar to this question ... What are the worst practices you actually found in
I actually posted this question because I found similar questions but not a perfect
I know there are actually a number of questions similar to this one, but
Somewhat academic question, but: How do value types like Int actually work ? I've
[I just posted a similar question, but I'm not sure whether is actually got
This question may be very similar to my one, but I cannot see the
Sorry I know similar question have been asked many times before but like most
This question is similar to this one: Restrict method access to a specific class
My question is similar to this one Multiply a 3D matrix with a 2D
My question is similar to this one: need to call soap ws without wsdl

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.