Skip to content

Instantly share code, notes, and snippets.

@NrI3
Created June 17, 2015 19:40
Show Gist options
  • Save NrI3/2ca53a3a3a5807a7a8df to your computer and use it in GitHub Desktop.
Save NrI3/2ca53a3a3a5807a7a8df to your computer and use it in GitHub Desktop.
[ Java ] Eliminar dígitos repetidos de un numero entero, tomando en cuenta los números de izquierda a derecha
public int EliminarDigitosRepetidos(int n){
int c = getCantidadDigitos(n);
int r = 0;
while(c>1){
int x = getPosition(1,n);
r = r * 10 + x;
int l = c;
int t = 0;
c--;
for(int p=2 ; p < l ; p++){
int y = getPosition(p,n);
if (x!=y)
t = t * 10 + y;
else
c--;
}
n = t;
}
return r;
}
public int getPosition(int x, int n){
int c = getCantidadDigitos(n);
if (x>c || x<1) return 0;
return (n/(int)Math.pow(10,c-x))%10;
}
public int getCantidadDigitos(int n){
int c = 0;
while(n>0){
c++;
n/=10;
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment