Searching and Counting elements of a sequence


You can just input in a SET and print the size of the set. Since duplicates are not allowed in the set.

Or use any other data structure to store and search each element and increment count if not found.(takes more time O(n^2))

I tried but code is not working it shows runtime error .please help me write the code

I’m guessing (??) that the spirit of the “do not use arrays in the code” is to come up with an O(1) space solution?

You can probably do this by just keeping track of the last number read and checking if the new number is equal to it - if not, you have a new distinct number since the input is sorted. Something along those lines :slight_smile:

I have tried it but code isn’t working
Write the code that works
I am not able to write code without arrays

Someone please write proper working code for this problem please :pray:

Check if current number differs from last, if yes add 1.

O(1) space complexity implementation

Set implementation

Spoonfeeding at its finest