Question #203376

Given an array of integers, find the one that appears an odd number of times.

There will always be only one integer that appears an odd number of times.


Expert's answer

#include <iostream>
int get_odd_element(int *array,int arr_length)
{
    int count{ 0 };
    int element;
    for (int i = 0; i < arr_length; i++)
    {
        element = array[i];
        count = 0;
        for (int j = 0; j < arr_length; j++)
        {
            if (element == array[j]) 
            {
                count += 1;
            }
        }
        if (count % 2 == 1) 
        {
            return element;
        }
    }
}


int main()
{
    int test[] = { 1, 2, 3, 1, 4, 9, 1, 8 ,87,99};
    std::cout << " A number that occurs an odd number of times: " << get_odd_element(test, sizeof(test) / sizeof(*test)) << std::endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS