JSFiddle - React, Tailwind, and code Playground

by Neels Grobler

HTML

<div id="result"></div>

JavaScript

var array = ["car", "plane", "plane", "car", "car"];

var count = {};

// Map each element in the array to a key in the object
for (var i = 0; i < array.length; i++) {
    if (typeof count[array[i]] === 'undefined') {
        count[array[i]] = 1;
    } else {
        count[array[i]]++;
    }
}

// Loop through the object and print out the counts
for (var i in count) {
    var txt = "'" + i + "'" + " was found " + count[i] + " times";
    console.log(txt);
    document.querySelector('#result').innerHTML += "<div>" + txt + "</div>";
}