DEV Community

loading...

Discussion on: 30-Day LeetCoding Challenge (Day-1)

Collapse
ac000 profile image
Andrew Clayton

While I like the XOR trick. Seeing as this is #c, here's some C code...

#include <stdio.h>

static const int array[] = { 3, 4, 9, 3, 9 };

int main(void)
{
        int i = 0;
        int sum = 0;
        int n = sizeof(array) / sizeof(array[0]);

        for ( ; i < n; )
                sum ^= array[i++];

        printf("%d\n", sum);

        return 0;
}
Collapse
late_riser profile image
Sadi Author

Nice code. My one is C++ though!

Collapse
ac000 profile image
Andrew Clayton

OK. I was maybe too subtle.

I was really just pointing out the fact that you posted C++ code to the #c topic, when you actually wanted the #cpp one...

Remember, C != C++, I see people conflating the two all too often...

Thread Thread
late_riser profile image
Sadi Author • Edited

Oh I see! My bad! Actually what happened is, I gave tag as 'c++' which I guess the editor cannot differentiate. It just omits the '++'. Now I have given cpp which works. Thanks for the correction though :)