How can I find the most common number using JavaScript?

If the user enters 5 rooms, say ... 4, 4, 7, 7, 4

. 4 happened 3 (most) times. So there should be a way out 4

.

How can I do this using JavaScript? I would really appreciate your help. Thank!

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

PS 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");

      

+3


source to share


5 answers


Create an array a

with lots of numbers using array literals :

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

      

Create a simple 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 length */

      

Loopa

through the array with for(;;)

-loop
and increment the counter. The counter is stored in a hash map at the facility o

.
(o[a[i]] || 0)

required for the first occurrence of the key: when not found, the undefined

value is used instead 0

. 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

that looks like this:

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

      

Then loopo

with for(.. in ..)

-loop
and find the maximum number of times.
At the bottom of the loop, the conditional ternary is used.. ? .. : ..

:

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

      



After this cycle m

is:

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

      

And the maximum value can be written with:

o[m];

      

The 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"); 

      

+4


source


No need to do two passes and then accumulates the maximum result:

var g = [4, 4, 7, 7, 4, 5, 6, 7, 8, 6, 5, 2, 2, 2, 3, 4, 5]; //your array

for (var t = {}, maxn = g[0], max = 0, gi, i = g.length; i--;) {
  if (max < (t[gi = g[i]] = (t[gi] || 0) + 1)) {
    max = t[gi];
    maxn = gi;
  }
}

document.write ('The number ' + maxn + ' occurs ' + max + 'times');

      

Edit



Nice solution, but OP probably needs some explanation and some more appropriate variable names. The most common value in the set is mode.

// Use any member of g to seed the mode
var mode = g[0];
// The number of times the current mode has occurred
var count = 0;
// Results object
var t = {};
var i = g.length;
var gi;

// Loop over all the members
while (i--) {

  // Get the value at i
  gi = g[i];

  // Keep track of how many times the value has been found  
  // If the number hasn't occured before, add it with count 1
  // Otherwise, add 1 to its count
  t[gi] = (t[gi] || 0) + 1;

  // Set the mode to the current value if it has occurred 
  // more often than the current mode
  if (count < t[gi]) {
    count = t[gi];
    mode = gi;
  }
}

alert('The mode is ' + mode + ' and occurs ' + count + ' times.');

      

If there is more than one mode, one found time counter from the end of the array wins.

+3


source


http://jsbin.com/ageyol/3/edit

var g = [4, 4, 7, 7, 4, 5, 6, 7, 8, 6, 5, 2, 2, 2, 3, 4, 5]; //your array
var t = {}; // object which contain the numbers as properties.

for (var i = 0; i < g.length; i++)
{
    if (!t[g[i]]) t[g[i]] = 0; //if the property doesnt exists , so create one with counter 0.
    t[g[i]]++; // also - increase the property VALUE.
}

var max = 0;

for (i in t)
{
    if (t[i] > max) max = t[i]; //check if property value is larger then the current MAX val.
    document.write(i + "  " + t[i] + "<br/>");
}
document.write(t[max]);

      

ps if there is more than max - so you must iterate.

+2


source


Sorting the array, then the same values ​​are next to each other, so you can just loop over them and search for the longest string:

arr.sort();

var maxValue, maxCount = 0, cnt = 1, last = arr[0];
for (var i = 1; i <= arr.length; i++) {
  if (i = arr.length || arr[i] != last) {
    if (cnt > maxCount) {
      maxCount = cnt;
      maxValue = last;
    }
    cnt = 1;
    if (i < arr.length) last = arr[i];
  } else {
    cnt++;
  }
}

      

+2


source


To get rid of one of the other answers, once you've formed your values ​​object, you can push the values ​​into an array and use Math.max

to get the top one. Usually Math.max

accepts a series of numbers, but if you use .apply

it can also accept array

. The same goes for Math.min

.

var o = { 1: 5, 2: 3, 3: 2, 4: 2, 5: 1 };

var out = [];
for (var k in o) {
  out.push(o[k]);
}

var upperbound = Math.max.apply(null, out); // 5

      

0


source







All Articles