My task was to receive an array of integers and then reassemble the array as required:
- The array size is to be of element index 0
- The next element (index 1) has the number of even distinct digits (digits that appear in only one integer input but not the rest).
- And the next pairs (if any) of elements will store the value of even digits and their number of occurrences in the given array.
After all the even pairs are exhausted, the next element will have the number of odd uncommon digits and then followed by the odd digits and their occurrences.
Input {1201, 6266, 35, 77}
Output {15, 2, 0, 1, 6, 3, 4, 1, 2, 3, 1, 5, 1, 7, 2}
Here is my brute force attempt. How can I improve my logic and shorten my code without the use of a vector?
#include <iostream>
using namespace std;
int* getUncommonDigitStatistics(int*, int);
int main() {
int testAry[] = { 1201, 6266, 35, 77 };
int size = 4;
int* result;
//int* result2 = getUncommonDigitStatistics(testAry, size);
result = getUncommonDigitStatistics(testAry, size);
for (int i = 0; i < result[0]; i++) {
cout << result[i] << endl;
}
return 0;
}
int* getUncommonDigitStatistics(int* input, int size) {
bool isCommon[10] = { false };
int digitCounter[10] = { 0 };
int* uncommonArray;
int newSize;
int evens = 0;
int odds = 0;
int currentInt;
int currentDigit;
int searchInt;
int searchDigit;
int i, j;
for (i = 0; i < size; i++) {
// For each integer
currentInt = *(input + i);
if (currentInt < 0) {
currentInt = -currentInt;
}
do {
currentDigit = currentInt % 10;
// Search each digit
if (digitCounter[currentDigit] == 0) {
// If we haven't looked already
if (isCommon[currentDigit] == false) {
// If it has not already been found common
for (j = 0; j < size &&
isCommon[currentDigit] == false; j++) {
//Search each other integer
if (j != i) {
// Don't search the same one!
searchInt = *(input + j);
if (searchInt < 0) {
searchInt = -searchInt;
}
do {
searchDigit = searchInt % 10;
// Check each digit
if (currentDigit == searchDigit) {
isCommon[currentDigit] = true;
}
searchInt /= 10;
} while ((searchInt > 0) &&
(isCommon[currentDigit] ==
false));
}
}
}
if (isCommon[currentDigit] == false) {
// We didn't find it
digitCounter[currentDigit]++;
}
}
else {
// We've already determined it's uncommon
digitCounter[currentDigit]++;
}
currentInt /= 10;
} while (currentInt > 0);
}
for (i = 0; i < 10; i++) {
// Count evens/odds. This should have been done in the
// previous step.
if (digitCounter[i] > 0) {
if (i % 2 == 0) {
evens++;
}
else {
odds++;
}
}
}
newSize = (3 + (evens * 2) + (odds * 2));
uncommonArray = new int[newSize];
*(uncommonArray) = newSize;
j = 2;
if (evens > 0) {
// Assign values for even digits
*(uncommonArray + 1) = evens;
for (i = 0; i < 10; i += 2) {
if (digitCounter[i] > 0) {
*(uncommonArray + j) = i;
*(uncommonArray + j + 1) = digitCounter[i];
j += 2;
}
}
}
else {
*(uncommonArray + 1) = 0;
}
*(uncommonArray + j) = odds;
j++;
if (odds > 0) {
// Assign values for odd digits
for (i = 1; i < 10; i += 2) {
if (digitCounter[i] > 0) {
*(uncommonArray + j) = i;
*(uncommonArray + j + 1) = digitCounter[i];
j += 2;
}
}
}
return uncommonArray;
}