Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Find the most common elements in array of objects
[{
    country: "US",
    languages: [
      "en"
    ]
  },
  {
    country: "BE",
    languages: [
      "nl",
      "fr",
      "de"
    ]
  },
  {
    country: "NL",
    languages: [
      "nl",
      "fy"
    ]
  },
  {
    country: "DE",
    languages: [
      "de"
    ]
  },
  {
    country: "ES",
    languages: [
      "es"
    ]
  }
]

given the above object, listing some countries objects, how can I find the most common official language(s), of all countries using javascript?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use a data structure that is well suited for lookups i. e. a Map or just a simple JavaScript Object (lookup in O(1)) and store all languages and a count of their occurrences there.

After you have done that, sort the occurrences and select the first item (when sorting in descending order) and you will have the item with the most occurrences.

Here an implementation using Map.

const data = [{
    country: "US",
    languages: [
      "en"
    ]
  },
  {
    country: "BE",
    languages: [
      "nl",
      "fr",
      "de"
    ]
  },
  {
    country: "NL",
    languages: [
      "nl",
      "fy"
    ]
  },
  {
    country: "DE",
    languages: [
      "de"
    ]
  },
  {
    country: "ES",
    languages: [
      "es"
    ]
  }
]

const countLanguages = new Map();
data.forEach(country => {
    country.languages.forEach(lang => {
        if(!countLanguages.has(lang)){
            countLanguages.set(lang, 1);
        }
        else countLanguages.set(lang, countLanguages.get(lang) + 1);
    })
    
})


const sorted = [...countLanguages.entries()].sort();
console.log("Most occurrences:", sorted[0])

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda