I need a fastest code that will tell me whether a number has duplicate digits or not…I don’t wanna use brute force technique coz that I have already tried.
Plz help
you can maintain an array of size 10…and each index represent a digit…to cater multiple occurrences of zero you can take the input as a string…after going through all the digits, if array[i] is greater than one then the digit has duplicate digit present else not…
int arr[10]={0};
input string;
len=legth of string;
i=0;
while(i < len)
{
arr[str[i]-48]++;
}
for(i=0;i < 10;i++)
{ if(arr[i]> 1)
"Duplicate exists"
}
1 Like