Answer to Question #203376 in C++ for Skye Jordan

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.


1
Expert's answer
2021-06-05T03:41:18-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog