Write a function to reverse a number by eliminating duplicates in it?
Ex:
input : 2452
output : 254
The idea is to have an array of size 10 (having all digits from 0-9) which keeps a note of whether any digit has its duplicate.
Code:int Reverse(int num) { int mod=0, ret=0; int arr[10]={0}; while(num) { mod=num%10; if(arr[mod]==0) { arr[mod]=1; ret=ret*10+mod; } num=num/10; } return ret; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks