diff --git a/src/countcities.ts b/src/countcities.ts new file mode 100644 index 0000000..c2bda15 --- /dev/null +++ b/src/countcities.ts @@ -0,0 +1,34 @@ +import rawData from "https://didier-ahuntsic.gitlab.io/cours-420-317-ah/data/cities.json" assert {type: "json"}; + +type CityWithCount = [string, number] + +function countOccurrences(cities: string[]): Map { + let ret = new Map(); + for(const city of cities) { + ret.set(city, (ret.get(city) ?? 0) + 1) + } + return ret; +} + +function maxOccurrence(occurrences: Map): CityWithCount { + let maxOccurrenceName = ""; + let maxOccurrenceValue = 0; + for(const [name, value] of occurrences.entries()){ + if(value > maxOccurrenceValue){ + maxOccurrenceName = name; + maxOccurrenceValue = value; + } + } + return [maxOccurrenceName, maxOccurrenceValue]; +} + + +function getMostListedCity(cities: string[]): CityWithCount{ + const occurrences: Map = countOccurrences(cities); + return maxOccurrence(occurrences); +} + +console.log(getMostListedCity(rawData as string[])); + + +