# JavaScript Challenge - Calculate the Mean and Mode, Calculate the Mean and Mode

Sorry, I wrongly “reply”. I just want to send my answer

The optimal solution should be O(n), cuz you iterate the array twice, first you calculate the occurances, second you try to retrive the largest occurance number. No recalculation.

function statsFinder(numbers) {
// Calculate mean
// Time complexity: O(n), where n is the number of elements in the array
const mean = numbers.reduce((sum, num) => sum + num, 0) / numbers.length;

``````// Calculate mode
// Time complexity: O(n), where n is the number of elements in the array
const count = {};
let maxCount = 0;
let mode = null;

for (const num of numbers) {
count[num] = (count[num] || 0) + 1;

if (count[num] > maxCount) {
maxCount = count[num];
mode = num;
}
}

return [mean, mode];
``````

}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

function statsFinder(numbers) {
// Calcular la media
const mean = numbers.reduce((acc, curr) => acc + curr, 0) / numbers.length;

// Calcular la moda
const occurrences = {};
let maxCount = 0;
let mode = null;
numbers.forEach(number => {
occurrences[number] = (occurrences[number] || 0) + 1;
if (occurrences[number] > maxCount) {
maxCount = occurrences[number];
mode = number;
}
});

// Crear una lista con la media y la moda
const result = [mean, mode];
return result;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

function statsFinder(array) {
// Write your code here
let modeOcc = 0;
let mode;
const uniqueValues = {};
let sum = 0;
array.forEach(value => {
sum += value;
if (uniqueValues[value]) {
uniqueValues[value]++;
} else {
uniqueValues[value] = 1;
}
if (uniqueValues[value] > modeOcc) {
modeOcc = uniqueValues[value];
mode = value;
}
})
const stats = [sum/array.length, mode];
return stats;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

function statsFinder(array) {
let mean = 0;
let g = 0;

let y=;
for(let i =0; i<array.length;i++ ){
mean = mean + array[i];
g++;
}
let a = mean / g;
let mode = ;
for(let i = 0; i < array.length;i++){
for(let j = 0; j < array.length ; j++){
if (i === j){
continue;
}else if (array[i] === array[j])
{
mode.push(array[i]);
break;
}
}
}
const hh = [a, mode[0]];
return hh;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;

any ideas to make my code shorter.

We’re going to go whole hog on exploring JS alternate methods to do this.

``````const func = function (s) {
const n = s.length
const u = s.reduce((a, b) => a + b) / n    //  mean
const d = {}
s.forEach(function (k) {
if (k in d) {
this[k] += 1
} else {
this[k] = 1
}
}, d)
const v = Array.from(Object.entries(d))
v.sort((a, b) => b[1] - a[1])
return [u, +v[0][0]]
}
console.log(func([500, 400, 400, 375, 300, 350, 325, 300]))
[368.75, 300]
``````

Maybe not simpler, just an alternate approach. It may be flawed. We’ll leave you to explore in the coming weeks.

1 Like

Variation on the above example, pared down:

``````const func = function (s) {
const u = s.reduce((a, b) => a + b) / s.length    //  mean
let d = {}
s.forEach(function (k) {
if (k in this) {
this[k] += 1
} else {
this[k] = 1
}
}, d)
d = Array.from(Object.entries(d))
d.sort((a, b) => b[1] - a[1])
return [u, +d[0][0]]
}

console.log(func([500, 400, 400, 375, 300, 350, 325, 300]))
// [368.75, 300]``````
``````function statsFinder(array) {
const media = array.reduce((acc, curr) => acc + curr, 0) / array.length

const moda = array.reduce((acc, curr) => {
if (curr in acc) {
acc[curr]++
} else {
acc[curr] = 1
}
return acc
}, {})

const modaArray = Object.keys(moda).reduce((a,b) => moda[a] > moda[b] ? a : b)

return [media, parseInt(modaArray)]
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````
function statsFinder(array) { const sum = array.reduce((a,b) => { return a + b; },0) const mean = sum / array.length; let mode = 0 let maxOccurances = 0 array.forEach((element) => { let occurance =0 for(let i =0;i < array.length; i++) { if(element === array[i]){ occurance += 1 } } if(occurance > maxOccurances){ maxOccurances = occurance; mode = element; } }) return [mean,mode]; } console.log(statsFinder([500,400,400, 375,300, 350, 325, 300])) // Leave this so we can test your code: module.exports = statsFinder;
``````function statsFinder(array) {
// Write your code here
let total = 0
const modes = {}
for(let i=0; i<array.length; i++) {
total += array[i]
if(modes[array[i]] === undefined) {
modes[array[i]] = 1
} else {
modes[array[i]] = modes[array[i]] + 1
}
}

let incremental = 0
for(let mode in modes) {
if(modes[mode] > incremental) {
incremental = modes[mode]
}
}

const tmp = []
for(let mode in modes) {
if(modes[mode] === incremental) {
tmp.push(mode)
}
}

let modeFinal = 0
for(let index of tmp) {
if(parseInt(index) > modeFinal) {
modeFinal = parseInt(index)
}
}

const mean = total / array.length

return [mean, modeFinal]
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````

Did this without using dictionaries.

``````function statsFinder(array) {
const sum = (array.reduce((p,c) => p + c, 0));
const mean = sum/array.length;
let mode = array[0];
let highest = 0;
for (let arr of array) {
let count = array.filter((e) => e == arr).length;
if (count > highest) {
highest = count;
mode = arr;
}
}
return [mean, mode];
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

// Leave this so we can test your code:
module.exports = statsFinder;
``````