Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 1.22 KB

_1426. Counting Elements.md

File metadata and controls

55 lines (41 loc) · 1.22 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 04, 2024

Last updated : July 01, 2024


Related Topics : Array, Hash Table

Acceptance Rate : 60.34 %


Solutions

C

// Oddly stated question
/* Wasnt sure if they wanted the repeat values to be like n*m, min(n,m), or whatnot
 * In the end tinkering found that what the test cases search for is just n as in n
 * can pair to any m values but if n=1, no matter how many m's there are n can only
 * pair to one.
 */

int countElements(int* arr, int arrSize) {
    short* temp = malloc(sizeof(short) * 1001);
    for (int i = 0; i < 1001; i++) {
        temp[i] = 0;
    }

    for (int i = 0; i < arrSize; i++) {
        temp[arr[i]]++;
    }

    int output = 0;
    for (int i = 0; i < 1000; i++) {
        if (temp[i] && temp[i + 1]) {
            output += temp[i];
        }
    }

    free(temp);
    return output;
}