Problem of the Day
A new programming or logic puzzle every Mon-Fri

Unique Integers

Happy Monday everyone!

Today's goal is to determine the amount of unique integers in the range [0-1000000]. A unique integer is an integer with no duplicates of numbers. 7 and 18 are unique whereas 99 and 100 are not. Your program should print out just the final count of total unique integers.

Permalink: http://problemotd.com/problem/unique-integers/

Comments:

  • Anonymous - 9 years, 10 months ago

    Final: 168,570

    class Program
    {
        static void Main(string[] args)
        {
            int uniqueNumbers = 0;
    
            for (int i = 1; i <= 1000000; i++ )
            {
                HashSet<char> chars = new HashSet<char>(i.ToString());
                if (chars.Count == i.ToString().Length)
                {
                    uniqueNumbers++;
                }
            }
    
            Console.WriteLine("Unique Integers: " + uniqueNumbers);
        }
    }
    

    reply permalink

  • David - 9 years, 9 months ago

    This has an off by one error for not including "0".

    reply permalink

  • Kevin Benton - 9 years, 10 months ago

    168571 len([x for x in xrange(0, 1000000) if len(str(x)) == len(set(str(x)))]) Another one that doesn't need a big list

    reduce(lambda count, item: count + 1 if len(str(item)) == len(set(str(item))) else count, xrange(0, 1000000), 0)
    

    reply permalink

  • Jtmach - 9 years, 10 months ago

    Final 168,571 don't forget about 0.

    Javascript

    this.window.onload = function () {
      var uniqueIntegers = 0;
      for (var index = 0; index < 1000000; index++) {
        if(checkString(index.toString())) {
          uniqueIntegers = uniqueIntegers + 1;
        }
      }
    
      this.document.writeln("Unique integers: " + uniqueIntegers);
    };
    
    function checkString(chkStr)
    {
      if (chkStr.length == 1) {
        return true;
      }
    
      for (var chkInt = 0; chkInt < 10; chkInt++)
      if (chkStr.indexOf(chkInt.toString()) != chkStr.lastIndexOf(chkInt.toString())) {
        return false;
      }
    
      return true;
    }
    

    reply permalink

  • Eddy - 9 years, 10 months ago

    My solution in C using only arithmetic and bitwise operations.

    reply permalink

  • David - 9 years, 9 months ago

    I decided to do this mathematically using permutations.
    One day I will get fancy and upload my scratch work up to git so I can just link it, like Eddy.
    Here is my counting in C.

    #include <stdio.h>   
    
    int factorial(int num) {   
        int prod = 1;   
        for(int i = 1; i <= num; i ++) {
                prod *= i;
            }
        return prod;
           }
    
    int permutation(int n, int r) {
        int perm = factorial(n);
        perm /= factorial(n-r);
        return perm;
    }   
    
    int main(void)
    {
        int uniques = 1;    //My default value accounts for 0 because of my method.
        for(int i = 1; i < 7; i++)
        {
            if(6 == i)
                uniques += permutation(9,i);    //Count the 6 digit permutations of [1,9]
            else
                uniques += (i + 1) * permutation(9,i);  //Count the i digit permutations of [1,9] and the i + 1 digit permutations that include 0
        }
        printf("%d\n", uniques);
    
        return 0;
    }   
    

    I needed to build my permutation function n!/(n-r)! so it took a little extra overhead in that regard. I ran the permutations of [1,9] for various numbers of digits and added in the cases for then 0 being inserted after any existing digit.

    reply permalink

Content curated by @MaxBurstein