Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eduardosilva/170ec8020a50fec41d0f2ee1a509cec4 to your computer and use it in GitHub Desktop.
Save eduardosilva/170ec8020a50fec41d0f2ee1a509cec4 to your computer and use it in GitHub Desktop.
useful algorithms
using System;
public class Program
{
public static void Main()
{
var n = 124123;
var countArray = new int[10];
do {
// get the last digit in the number
var rightmostDigit = n % 10;
Console.WriteLine("rightmostDigit:" + rightmostDigit);
countArray[rightmostDigit]++;
// remove the last digit in the number
n = n / 10;
Console.WriteLine("n:" + n);
} while(n != 0);
// now that we have stored the occurrences
// of each of the digits inside the `countArray`,
// we just have to loop through the elements
// of that array
for(var i = 0; i < 10; i++) {
// check if the value is greater than 1 (this
// digit existed more than once, hence a duplicate)
if(countArray[i] > 1) {
// take note that the index represents the digit,
// while the element represents how many times
// that digit has occurred
Console.WriteLine("Digit " + i + " existed " + countArray[i] + " times\n");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment