Created
September 9, 2021 21:27
-
-
Save tayhsn/bc62d556aabe95ee0e92c43dc693035e to your computer and use it in GitHub Desktop.
Exercício lógico com Maps JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dada uma lista de documentos e seus respectivos status, escreva um código que informe qual é o status encontrado com maior frequência e o status encontrado com menor frequência na lista. | |
Exemplo de input: | |
[{"id":1,"status":"CI"},{"id":2,"status":"EE"},{"id":3,"status":"EP"},{"id":4,"status":"ES"},{"id":5,"status":"ER"},{"id":6,"status":"ET"},{"id":7,"status":"CI"},{"id":8,"status":"EE"}] */ | |
const lista = [ | |
{"id":1,"status":"CI"}, | |
{"id":2,"status":"EE"}, | |
{"id":3,"status":"EP"}, | |
{"id":4,"status":"ES"}, | |
{"id":5,"status":"ER"}, | |
{"id":6,"status":"ET"}, | |
{"id":7,"status":"CI"}, | |
{"id":8,"status":"EE"}, | |
{"id":9,"status":"CI"}, | |
] | |
var mapa = new Map(); | |
for(var i=0; i< lista.length; i++) { | |
if(mapa.has(lista[i].status)){ | |
let quanto_tem = mapa.get(lista[i].status) | |
mapa.set(lista[i].status, quanto_tem + 1) | |
} else{ | |
mapa.set(lista[i].status, 1) | |
} | |
} | |
console.log(mapa) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment