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 9077339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:19:02+00:00 2026-06-16T19:19:02+00:00

I have written a relatively convoluted implementation of Microsoft Excel’s MODE.MULT function, which returns

  • 0

I have written a relatively convoluted implementation of Microsoft Excel’s MODE.MULT function, which returns an array of the most frequently occurring, or repetitive values in an array. It’s implemented with three loops, including one nested into another, and I’m suspecting that there is a simpler way to go about it. For information, it’s using _.uniq from Lo-Dash for extracting duplicate values out of the returned array.

function MODEMULT(range) {
  var n = range.length;
  var max = 0;
  var counts = new Array();
  var result = new Array();
  for (var i = 0; i < n; i++) {
    counts[i] = 0;
    for (var j = 0; j < n; j++) {
      if (range[j] === range [i]) {
        counts[i]++;
        max = (counts[i] > max) ? counts[i] : max;
      }
    }
  }
  for (var k = 0; k < n; k++) {
    if (counts[k] === max) {
      result.push(range[k]);
    }
  }
  return _.uniq(result);
}

For testing purposes, MODEMULT([1,2,3,4,3,2,1,2,3]) should return [2,3]

Thanks in advance for your help!

  • 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-06-16T19:19:04+00:00Added an answer on June 16, 2026 at 7:19 pm

    You could go with less loops, although there is an impact on memory usage as you will keep counts on all unique entries in the original range:

    function MODEMULT(range) {
      var n = range.length,
          // object to hold the number of occurrences per entry
          count= {},
          // array to hold those numbers which have the current max occurrences 
          maxItems = [],
          // reference to the current maximum
          max = 0,
          // JSLint placement of currentItem used inside the loop
          currentItem;
    
      for (var i = 0; i < n; i++) {
        currentItem= range[i];
    
        // Update the occurrences table.
        count[currentItem] = count[currentItem] ? count[currentItem] + 1 : 1;
    
        // If a new maximum is created, void the original maxItems and update max.
        if (count[currentItem] > max) {
           max = count[currentItem];
           maxItems = [];
        }
    
        // If the entry reaches the current max, add it to maxItems.
        if (count[currentItem] === max) {
            maxItems[maxItems.length] = currentItem;
        }
      }
    
      // No need for unique, as a duplicate value
      // will increase max and thus empty out maxItems.
      return maxItems;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function which fires when a particular menu item is clicked.
I have written a relatively simple Java App Engine application which I would like
I am relatively new to oracle and have written the merge statment below which
I have written some relatively simple jQuery plug-ins, but I am contemplating writing something
I have written a relatively big insert statement, and some of the fields are
have written this little class, which generates a UUID every time an object of
I have written a bash script which installs a number of packages, however for
I have written a function that sorts a big scale of data. To test
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I am still relatively new to unit testing. I have written a class in

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.