Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3808608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:10:25+00:00 2026-05-19T15:10:25+00:00

Let’s say I want to check if a number n = 123 has duplicate

  • 0

Let’s say I want to check if a number n = 123 has duplicate digits. I tried:

#include <iostream>

using namespace std;

int main() {
    int n = 123;
    int d1 = n % 10;
    int d2 = ( n / 10 ) % 10;
    int d3 = ( n / 100 ) % 10;
    if( d1 != d2 && d1 != d3 && d2 != d3 ) {
        cout << n << " does not have duplicate digits.\n";
    }
}

Is there any faster solution to this problem?

Update
Sorry for being unclear. The code above was written in C++ only for description purpose. I have to solve this problem in TI-89, with a number of 9 digits. And since the limitation of memory and speed, I’m looking for a fastest way possible.

TI-89 only has several condition keyword:

  • If
  • If … Then
  • when(
  • For … EndFor
  • While … EndWhile
  • Loop … EndLoop
  • Custom … EndCustom

Thanks,
Chan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:10:26+00:00Added an answer on May 19, 2026 at 3:10 pm

    Not necessarily faster but you should measure anyway, just in case – my optimisation mantra is "measure, don't guess".

    But I believe it’s clearer in intent (and simple enough to be translated to a simpler calculator language. It’s also able to handle arbitrarily sized integers.

    int hasDupes (unsigned int n) {
        // Flag to indicate digit has been used, all zero to start.
        int used[10] = {0};
    
        // More than 10 digits must have duplicates, return true quickly.
        if (n > 9999999999) return 1;
    
        // Process each digit in number.
        while (n != 0) {
            // If duplicate, return true as soon as found.
            if (used[n%10]) return 1;
    
            // Otherwise, mark used, go to next digit.
            used[n%10] = 1;
            n /= 10;
        }
    
        // No duplicates after checking all digits, return false.
        return 0;
    }
    

    If you have a limited range of possibilities, you can use the time-honoured approach of sacrificing space for time. For example, let’s say you’re talking about numbers between 0 and 999 inclusive (the : : markers simply indicate data I’ve removed to keep the size of the answer manageable):

    const int *hasDupes = {
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  //   0 -   9
            0, 1, 0, 0, 0, 0, 0, 0, 0, 0,  //  10 -  19
            0, 0, 1, 0, 0, 0, 0, 0, 0, 0,  //  20 -  29
                        :  :
            0, 0, 1, 0, 0, 1, 0, 0, 0, 0,  // 520 - 529
                        :  :
            0, 1, 0, 0, 0, 0, 0, 0, 1, 0,  // 810 - 819
                        :  :
            0, 0, 0, 0, 0, 0, 0, 1, 0, 1,  // 970 - 979
            0, 0, 0, 0, 0, 0, 0, 0, 1, 1,  // 980 - 989
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  // 990 - 999
    };
    

    and just do a table lookup of hasDupes[n]. The table itself could be generated (once) programmatically and then just inserted into your code for usage.

    However, based on your edit where you state you need to handle nine-digit numbers, a billion-element array is probably not going to be possible on your calculator. I would therefore opt for the first solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class library project that has any number of supplemental
Let's say that each Product has a category. I want to ask the Users
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I want to use Breeze to create a Task entity (I'm using
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let say I want to create an iOS app that download music files from
Let's say I have a 1 GB text file and I want to read
Let's say I have an actor like this (Using Akka 2.1 with Scala 2.10):
Let's say I have one class User, and it has a property of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.