UNPKG

@thumbmarkjs/thumbmarkjs

Version:

![GitHub package.json dynamic](https://img.shields.io/github/package-json/version/ilkkapeltola/thumbmarkjs) ![NPM Version](https://img.shields.io/npm/v/@thumbmarkjs/thumbmarkjs) ![NPM Downloads](https://img.shields.io/npm/dm/%40thumbmarkjs%2Fthumbmarkjs

39 lines (31 loc) 1.38 kB
function mostFrequentValue(arr: any[]): any | null { if (arr.length === 0) { return null; // Return null for an empty array } const frequencyMap: { [key: string]: number } = {}; // Count occurrences of each element in the array arr.forEach((element) => { const key = String(element); frequencyMap[key] = (frequencyMap[key] || 0) + 1; }); let mostFrequent: any = arr[0]; // Assume the first element is the most frequent let highestFrequency = 1; // Frequency of the assumed most frequent element // Find the element with the highest frequency Object.keys(frequencyMap).forEach((key) => { if (frequencyMap[key] > highestFrequency) { mostFrequent = key; highestFrequency = frequencyMap[key]; } }); return mostFrequent; } export function mostFrequentValuesInArrayOfDictionaries(arr: { [key: string]: any }[], keys: string[]): { [key: string]: any } { const result: { [key: string]: any } = {}; keys.forEach((key) => { const valuesForKey = arr.map((obj) => (key in obj ? obj[key] : undefined)).filter((val) => val !== undefined); const mostFrequentValueForKey = mostFrequentValue(valuesForKey); if (mostFrequentValueForKey) result[key] = mostFrequentValueForKey; }); return result; }