pawan chopra wrote:I was writing an algorithm to find unique objects in a array using Java. I have written two methods one is recursive and other is having nested loops. According to the time taken recursive is quite slow and also its takes more memory because each recursion will have a new stack allocated to the method. But in other case it was quite fast. Am I right about recursion ? If yes then what is the use of recursive functions? I read some where its easy to use and make it look simple.
If memory usage is the issue you can use
Tail Recursion. Though it is practically not possible to change every recursive function into tail recursion but a lot depends on your requirements and implementation.
Also it will be good to share that what are you trying to do. If you are only trying to identify the unique Objects in a Collection then in both of the cases the Complexity of time will be the same. O(n)