requirement: return sorted, no dup integer array. return size 0 array if no common found.
I have implemented in the following way:
1. create a third array to store common int . set size to max(array a, array b)
2. sort both array by quicksort
3. user for loop to find common value and store to third loop
4. fix integer 0 problem. when int array is initialize it put 0 . So I need to fix this
5. eliminate dup by run a for loop
This is definitely not the best way in term of time complexity. Can anyone provide better solution ?
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.
Joined: Oct 22, 2009
Here's another more C-like approach. It's linear but requires the input arrays to be sorted.
Compare with the complexity of a binary search for example. It's generally considered O(lnN) but that's only because the dataset is specified to be initially sorted. If the sorting was considered part of the algorithm then also a binary search would be O(N * lnN).
That's why I emphasized in my post that the input arrays were to be considered intially sorted. I wrote
It's linear but requires the input arrays to be sorted.
This means that the algorithm is linear or O(N) if the input arrays are sorted. Otherwise it won't even work, just like a binary search won't work if the dataset isn't sorted.
I hope this clarifies your doubts. If you have further questions please feel free to ask. I sinecerely applogize for any inconvenience you may have suffered due to my hard to understand sentence above.
Joined: Oct 13, 2005
Yes, that clarifies the point Thank you.
subject: Question about finding common integer from two integer array