In my previous posts in finding duplicates series here and here we have seen different variation of this problem. This is another interesting twist in the problem as now we have given input which falls in a given range.
Let's walk through an example-:
Below we are given an example where an array given contains elements that are in range 1-10.
When we have given input in a range, counting sort like algorithm is the best we can implement. More on counting sort is here
Inspired from counting sort algo, to solve this algorithm we’ll create another count array in which we store count of each item at the index i.e. at index 2 we’ll store item 2 in the count array.
Here is repl in Java.
We have learnt another variation of finding duplicate problem plus we also learnt about counting sort. In coming posts I want to work on String related problems.
Happy coding. Cheers.