“These are the most common interview questions all time. Remove duplicates in an string, Find whether there is a duplicate etc. How to attach these problems?”Question & Code
1. Find whether a string contains unique items. The string contains only lower case letters.
2. Find whether the integer array contains unique numbers. The integer value in array can be negative and also can be 32-bit size.
3. What if the integer input in array is huge? shift operation will overflow? how does it work for negative numbers?
4. How to remove duplicate characters from an array without using any additional data structure or space. You can only use temporary variables
An hacky & confusing code
But for the above code, there is no constant space bargain necessary. A cleaner code would be to use a bool array for 256 bytes and use it to just add unique characters to the top of the array. All non-unique characters never reach the top.
You can also have taken the negative case in the above code. If the character is not repeated, then we can simple move *str to a the top of the array.
5. The same question might be asked for integer array as well. The simple approach is to confirm that the integer is not repeated using bitwise, and simply but that in lower indices