# Find The Parity Outlier [6 kyu] [Find The Parity Outlier](https://www.codewars.com/kata/5526fc09a1bbd946250002dc) 6 kyu ## Solution ```cpp= /** *** Author: R-CO *** E-Mail: daniel1820kobe@gmail.com *** Date: 2020-07-22 **/ #include <cstdlib> #include <iostream> using std::cout; using std::endl; #include <vector> using std::vector; struct TestCaseStruct { vector<int> input; int expected_output; }; class FindTheParityOutlier { public: int FindOutlier(std::vector<int> arr) { int result; int odd_count = 0; int even_count = 0; int last_odd; int last_even; for (auto number : arr) { if ((number & 0x1) == 0) { last_even = number; ++even_count; if (even_count > 1 && odd_count == 1) { break; } } else { last_odd = number; ++odd_count; if (odd_count > 1 && even_count == 1) { break; } } } if (odd_count == 1) { result = last_odd; } else if (even_count == 1) { result = last_even; } return result; } }; int main(int argc, char *argv[]) { /* [ 2, 4, 0, 100, 4, 11, 2602, 36 ] Should return : 11(the only odd number) [160, 3, 1719, 19, 11, 13, -21] Should return : 160(the only even number) */ TestCaseStruct test_cases[] = {{{2, 4, 0, 100, 4, 11, 2602, 36}, 11}, {{160, 3, 1719, 19, 11, 13, -21}, 160}}; int index = 0; FindTheParityOutlier solution; for (auto &test_case : test_cases) { if (solution.FindOutlier(test_case.input) == test_case.expected_output) { cout << "test case \"" << index++ << "\" is pass." << std::endl; } else { cout << "test case \"" << index++ << "\" is fail." << std::endl; } } return EXIT_SUCCESS; } ``` ## Result PASS ###### tags: `CodeWars` `C++`