JSFiddle - React, Tailwind, and code Playground

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

function retornarOcorrencias(str){
    var ocorrencias = {};
    for(var i = 0; i < str.length; i++){
        var n = str[i];
        ocorrencias[n] = ocorrencias[n] ? ocorrencias[n] + 1: 1;
    }
    return ocorrencias;
}

var str = 'Goiaba';
var num = '001549954607410';

var strItens = retornarOcorrencias(str);
var numItens = retornarOcorrencias(num);

console.log(strItens);
console.log(numItens);