Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!