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?
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?
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);
}
}
}