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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:45:38+00:00 2026-06-01T13:45:38+00:00

If a user enters 5 numbers, lets say… 4, 4, 7, 7, 4 .

  • 0

If a user enters 5 numbers, lets say… 4, 4, 7, 7, 4. 4 occurred 3 (most number of) times. So the output should be 4.

How can I do this using JavaScript? Would much appreciate your help.
Thanks!

I’ve tried this so far. It works, but it’s too long, looking for something short and simple way.

P.S. This is not my homework!

    var n = parseInt(prompt("How many numbers do you like to enter?", ""));
    var num = new Array();

    for (i = 1; i <= n; i++) {
        num[i] = parseInt(prompt("Enter a number", ""));
        document.write("Entered numbers are: " + num[i] + "<br/>");
    }

    var total = new Array();
    for (i = 1; i <= n; i++) {
        var count = 1;
        for (j = i + 1; j <= n; j++) {
            if (num[i] == num[j]) {
                count++;
            }
            total[i] = count;
        }
    }

    var most = 0;
    for (i = 0; i < n; i++) {
        if (most < total[i]) {
            most = total[i];
        }
        var val = i;
    }
    document.write("<br/>" + num[val] + " is occurred " + most + " times");
  • 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-01T13:45:40+00:00Added an answer on June 1, 2026 at 1:45 pm

    Create an array a with a lot of numbers, using array literals:

    var a = [1, 2, 3, 4, 4, 5, 1, 2, 3, 1, 2, 1, 1];
    

    Create a plain object o, using object literals.

    var o = {},          /* Creates a new object */
        i = 0,           /* Declare variable i, initialise value at 0*/
        m = {m:0,t:null},
        t,               /* Declare variable t */
        len = a.length;  /* Declare variable len, set value to the array's length */
    

    Loop through array a using a for(;;)-loop and increment the counter. The counter is stored in a hashmap on object o.
    (o[a[i]] || 0) is needed for the first occurrence of the key: When it’s not found, the value 0 is used instead of undefined. See also Short-circuit evaluation: Logical OR.

    for ( ; i < len ; i++ ) {
        o[ a[i] ] = ( o[ a[i] ] || 0 ) + 1;
    }
    

    Then you have an object o which looks like:

    o = {
        "1": 5,
        "2": 3,
        "3": 2,
        "4": 2,
        "5": 1
    }
    

    Then loop through o using a for(.. in ..)-loop and find max times presented.
    At the bottom of the loop, the conditional ternary .. ? .. : .. operator is used:

    for ( i in o ) {
        t = { 
            m: o[i], 
            i: i 
        };
        m = m.m < t.m ? t : m;
    }
    

    After this loop m is equal to:

    m = { 
        i: "1", 
        m: "5"
    };
    

    And the maximum value can be captured using:

    o[m];
    

    witch gives you:

    5
    

    DEMO

    http://jsbin.com/utiqey/

    var a = [1, 2, 3, 4, 4, 5, 1, 2, 3, 1, 2, 1, 1]; 
    
    var o = {}, 
        i = 0, 
        m = {m:0,t:null}, 
        t,
        len = a.length; 
    
    for ( ; i < len ; i++ ) { 
        o[ a[i] ] = ( o[ a[i] ] || 0 ) + 1; 
    } 
    
    
    for ( i in o ) { 
        t = { 
            m: o[i], 
            i: i 
        };
        m = m.m < t.m ? t : m;
    } 
    
    alert(m.i + " is the highest presented " + m.m + " times"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this problem, the user enters in two numbers. Each number represents an integer,
I have a textbox where the user enters a number, but how can i
I'm trying to build a program that goes like this: User enters 3 numbers.
Overall goal: User enters some number in minutes and I change those numbers from
This is a game were a user enters a random number, then clicks Play,
I am trying to create a program where a user enters four numbers using
How do I check and see if a user enters only numbers and is
I have a button and an edittext. The user enters a number and then
This is what I want : Let the user enter as many numbers as
the below code is working fine if the user enters the USN number that

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.