Javascript Array find most common word
var mostCommonWord = function(arr){ var obj = {},/*from ww w .j a v a 2s. co m*/ placeHolder, max = 0; for(var i = 0; i < arr.length; i++){ if(obj[arr[i]] === undefined){ obj[arr[i]] = 1; }else{ obj[arr[i]]++; } if(obj[arr[i]] > max){ max = obj[arr[i]]; placeHolder = arr[i]; } } return [placeHolder, max]; }; console.log(mostCommonWord(['pear', 'apple', 'orange', 'pear','apple', 'pear'])); console.log(mostCommonWord(['pen', 'book', 'appl', 'ipad', 'pen', 'book']));