howie jao wrote:Can anyone provide better solution ?
What you want is the sorted intersection set between two sets.
I don't know if it's better but you can make use of the standard
Java collections, like
I choose a TreeSet for set "a" because you want the output sorted. The "b" set is a HashSet for speed. Doubles are automatically removed when entered into a Set.
There's still a little work to do. The sets must be loaded from the int arrays and afterwards the "a" set must be dumped into an int array.
Well, it's probably faster to use two HashSets and then sort the result int array at the end. Then the algorithm will be linear all the way up to the sort. And hopefully there aren't that many elements left to sort so it will be fast although sorting isn't a linear operation.