Sunday, 12 January 2014

Java problem to delete all duplicate digits from an input number?

Java problem to delete all duplicate digits from an input number?

Question:Write a program in java to delete all duplicate digits from an input number?

 Eg: if input is 112354 then output will be 12354 

Answer

import java.util.*;
public class duplicate {

public static void main(String args[]) 
{ long n;
int i=0,l=0,k=0;

Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
n=sc.nextLong();
String s=Long.toString(n);
char c[]=new char[s.length()];
ArrayList cc=new ArrayList();
String temp = Long.toString(n);
int[] arr = new int[temp.length()];
for (int m = 0; m< temp.length(); m++)
{
arr[m] = temp.charAt(m) - '0';
}
for(i=0;i<s.length();i++)
{
for(int j=0;j<s.length();j++)
{

if(arr[i]==arr[j]&&i!=j)
{
l=1;
}

}
if(l==0)
{
cc.add(arr[i]);
k++;
}
l=0;
}
Iterator itr=cc.iterator();
while(itr.hasNext())
{
Object ans=itr.next();
System.out.print(ans);

}
}
}


Posted By:Tanuj Kumar 
On:Shareyourconscience

0 comments:

Post a Comment