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 4097724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:12:37+00:00 2026-05-20T20:12:37+00:00

I have some int value its name is number . I have other int

  • 0

I have some int value its name is number. I have other int value its name is x. I want to know is x exist in number or not. Currently I convert number to string() and use string.Contain(x) method. I think this is not a good way, it does boxing and performance is hurt.

Are there a better way to do this?

Additional information:
number may be one or more digit, for example, 12345. x is always one digit.

  • 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-20T20:12:38+00:00Added an answer on May 20, 2026 at 8:12 pm

    That’s not a bad method. The only alternative I can think of is something using repeated divisions and modulus operators which may well be slower due to the quantity of those operations.

    I’d stick with what you have unless there’s a serious performance problem.


    Based on your additional information:

    number may be one or more digit, for example, 12345. x is always one digit.

    I would opt for the following (pseudo-code):

    def numContains (number, digit):
        if number == 0 and digit == 0:
            return true
        while number != 0:
            if number % 10 == digit:
                return true
            number = number / 10
        return false
    

    The first if is required since you don’t enter the while if you pass in a number of 0 and you still have to catch the case where both number and digit are 0.

    Otherwise, you just continue to check the least significant digit of number against digit, and divide number by 10 each time.

    If a match is found before number reaches zero, it contains the digit. Otherwise it doesn’t.

    This will probably be faster than your string solution, since it will have to do similar operations to create the string from the integer and then do the string compare on top of that.

    But, as with all optimisations, measure, don’t guess!


    And, now that I have access to my VS2008 development box, her’s some C# code for it:

    // Function: containsDigit, returns whether non-negative number holds a digit.
    //       In: num, the integer to check.
    //           dgt, the digit to look for.
    //      Out: Boolean representing whether digit found in number.
    //    Notes: Digit is coerced to a single digit.
    
    Boolean containsDigit(UInt32 num, UInt32 dgt) {
        dgt = dgt % 10;                // silently force contract compliance.
        if ((num == 0) && (dgt == 0))  // Zero contains zero.
            return true;
        while (num != 0) {             // While more digits in number.
            if ((num % 10) == dgt)     // Return true if rightmost digit matches.
                return true;
            num = num / 10;            // Get next digit into rightmost position.
        }
        return false;                  // No matches, return false.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string value and Type of some variable (For example int and 32
I have some code static void Main(string[] args) { int j = 0; for
I have some specific id like 1,2,5,11,64589 in solr (int type) I want to
I have some numbers stored in a std::vector<int> . I want to find which
I have this login script, but for some reason its not working. I checked
I have some legacy SQL (SP) declare @FactorCollectionId int; select @FactorCollectionId = collectionID from
I have some financial data gathered at a List[(Int, Double)], like this: val snp
I have some code that looks like: template<unsigned int A, unsigned int B> int
Say I have some windows method and a struct: struct SomeStruct{ int foo; int
I have some codes like this: PrintWriter pw = new PrintWriter(new BufferedReader(....)); for(int i=0;

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.