Created
September 29, 2023 15:33
-
-
Save lonelycompiler/aefcf9e21038912f96662cafa1d9ebcc to your computer and use it in GitHub Desktop.
Given an array of size length, return an array of [mean, mode]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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