Find the most frequent item of an array (not just strings)

后端 未结 7 2197
抹茶落季
抹茶落季 2020-12-10 19:42

Can someone walk me through this exercise? Write a JavaScript program to find the most frequent item of an array.

7条回答
  •  情话喂你
    2020-12-10 20:29

    Please try this one.

    var mostFrequnet = null,mostFrequnetItem ;
    var arr1 = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
    arr1.sort();
    
    
    for (var i=0;i mostFrequnet) {
    
             mostFrequnetItem = arr1[i];
             mostFrequnet = total;
             i= arr1.lastIndexOf(single)+1;
    
        }
    }
    
    console.log(mostFrequnet);
    
    console.log(mostFrequnetItem);
    

提交回复
热议问题