Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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



This post first appeared on Share Your Conscience: A Knowledge Sharing Place, please read the originial post: here

Share the post

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

×

Subscribe to Share Your Conscience: A Knowledge Sharing Place

Get updates delivered right to your inbox!

Thank you for your subscription

×