Ryu
Ryu

Reputation: 45

How to sort an array based on amount of unusual digits

I have to write a function that accepts an array of integers as arguments and display back to the user the "unusual" digits. Digits that appear only in one integer and not the rest, and then sort the array so that the integer with the largest occurrences of unusual digits is to be moved to the first element of the array, then followed by the integer with the next largest number of occurrences of unsual digits.

Input:

113
122
1000

Output:
There is 3 unusual digits:

0 occurs 3 times in 1000
2 occurs 2 times in 122
3 occurs 1 time in 113

Sorted:

1000
122
113

My question is how can I retrieve the integers that were associated with the unusual digits so that I can sort them in the future?

I would like to know which integer digit 0 came from and how many times it occurred in that integer.

Here's what I have so far, I apologize if the code is bad. I am not allowed to use any additional libraries other than iostream and all function calls must be written myself.

#include <iostream>

using namespace std;

void getUncommon(int* iAry, int size) {
    const int size2 = 10;
    int* tmpAry = new int[size];
    int totalCount[size2] = { 0 };
    int currentCount[size2] = { 0 };
    int totalUncommon = 0;
    int i, j;

    for (i = 0; i < size; i++) {
        tmpAry[i] = iAry[i];
        if (tmpAry[i] < 0)
            tmpAry[i] *= -1;

        for (j = 0; j < size2; j++)
            currentCount[j] = 0;

        if (tmpAry[i] == 0) {
            currentCount[0] = 1;
        }

        while (tmpAry[i] / 10 != 0 || tmpAry[i] % 10 != 0){
            currentCount[tmpAry[i] % 10] = 1;
            tmpAry[i] /= 10;
        }

        for (j = 0; j < size2; j++) {
            totalCount[j] += currentCount[j];
        }
    }

    for (i = 0; i < size2; i++) {
        if (totalCount[i] == 1) {
            totalUncommon++;
        }
    }

    cout << "Total of uncommon digits: " << totalUncommon << endl
        << "Uncommon digits:\n";
    if (totalUncommon == 0) {
        cout << "\nNo uncommon digits found.";
    }
    else {
        for (i = 0; i < size2; i++) {
            if (totalCount[i] == 1) {
                cout << i << endl;
            }
        }
    }

    return;
}

int main(){
    int* my_arry;
    int size;
    int i;

    cout << "How many integers? ";
    cin >> size;
    my_arry = new int[size];

    for (i = 0; i < size; i++) {
        cout << "Enter value #" << i + 1 << " : ";
        cin >> my_arry[i];
    }

    cout << "\nThe original array:" << endl;
    for (i = 0; i < size; i++) {
        cout << my_arry[i] << endl;
    }

    cout << "\nCalling function -\n" << endl;

    getUncommon(my_arry, size);

    delete[] my_arry;

    return 0;
}

Thanks ahead of time.

Upvotes: 1

Views: 534

Answers (1)

Vikas Marda
Vikas Marda

Reputation: 36

You can create a map with the digits 0 1 2 ... 9 as the key, and a pair of pointer to/index of integer containing the digit and number of occurrences of the digit in the integer as the value of the key-value pair.

Start iterating on the integer list, extracting the digits and their number of occurrences from each integer. You can do that by either using the modulo operator, or using string functions (after converting the integer to string). Now, for each integer, access the map of digits for all the digits in the integer, and if the value is uninitialised, update the value with the pointer/index to this integer and the number of occurrences of the digit in this integer. If the map entry is already populated, that means that it's not an "unusual" digit. So you can mark that map entry with a marker that conveys that this particular digit is not "unusual" and hence no need to update this entry.

After iterating over the entire integer list in this manner, you can iterate the map to find out which digits are unusual. You can also access the containing integer from the pointer/index in the value portion of the map's key-value pair. You can sort these entries easily using any sorting algorithm (since the number of values to be sorted is very small, no need to worry about time complexity, pick the easiest one) on the number of occurrences value.

Upvotes: 1

Related Questions