Skip to content

Instantly share code, notes, and snippets.

@lonelycompiler
Created September 29, 2023 15:33
Show Gist options
  • Select an option

  • Save lonelycompiler/aefcf9e21038912f96662cafa1d9ebcc to your computer and use it in GitHub Desktop.

Select an option

Save lonelycompiler/aefcf9e21038912f96662cafa1d9ebcc to your computer and use it in GitHub Desktop.
Given an array of size length, return an array of [mean, mode]
/*
Given an array of size length, return an array of [mean, mode]
*/
typedef struct nums nums
{
int *data;
int size;
}
int* meanAndMode (nums arr)
{
int mean = 0;
int mode = 0;
for (int i = 0; i < nums.size; i++)
{
for (int j = 0; j < nums.size; j++)
{
// if its null, meaning it was previously indexed to mode, skip it
if (nums.data[j] == NULL) continue;
if (nums.data[i] == nums.data[j])
{
mean += nums.data[j];
// add nums.data[j] to mode and then set nums.data[j] to NULL
}
}
}
mean /= nums.size;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment