-4

I have to enter a number(int) from the console and to check for a repeating digits in this number, but I must not use an array, and here I find it difficult.

Can anyone help me with a code?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    Show your code that attempts to solve this problem. Without your code the question will likely be closed without an answer. [mcve] – drescherjm Nov 14 '19 at 19:46

1 Answers1

0

You could use an integer to store which digits were entered:

#include <cstdint>
#include <iostream>

class DigitContainer {
public:
    bool contains(std::uint8_t digit) {
        return container & 1 << digit;
    }

    void set(std::uint8_t digit) {
        container |= 1 << digit;
    }
private:
    std::uint16_t container{};
};

int main() {
    DigitContainer keyPress;
    DigitContainer printed;
    std::uint64_t number;
    std::cin >> number;
    std::cout << "Repeated:\n";
    for (; number; number /= 10) {
        std::uint8_t digit = number % 10;
        if (keyPress.contains(digit)) {
            if (!printed.contains(digit)) {
                std::cout << static_cast<int>(digit) << "\n";
                printed.set(digit);
            }
        } else {
            keyPress.set(digit);
        }
    }
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62