JSFiddle - React, Tailwind, and code Playground

by Göran Andersson

HTML

<script>count()</script>

JavaScript

function count() {

    array_elements = ["2", "1", "2", "2", "3", "4", "3", "3", "3", "5"];

    array_elements.sort();

    var current = null;
    var cnt = 0;
    for (var i = 0; i < array_elements.length; i++) {
        if (array_elements[i] != current) {
            if (cnt > 0) {
                document.write(current + ' comes --> ' + cnt + ' times<br>');
            }
            current = array_elements[i];
            cnt = 1;
        } else {
            cnt++;
        }
    }
    if (cnt > 0) {
        document.write(current + ' comes --> ' + cnt + ' times');
    }

}