var myArr = ['apple', 'apple', 'orange', 'apple', 'banana', 'orange', 'pineapple'];
var obj = {};
myArr.forEach(function(item) {
if (typeof obj[item] == 'number') {
obj[item]++;
} else {
obj[item] = 1;
}
});
document.getElementById('output').innerHTML = Object.keys(obj).map(function(item) {
return item + (obj[item] == 1 ? '' : ' ' + obj[item]);
}).join('\n');