Forums Register Login

Question about finding common integer from two integer array

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Here's another more C-like approach. It's linear but requires the input arrays to be sorted.

+Pie Number of slices to send: Send
Are you sure it's linear complexity? Arrays.sort is merge sort, and that runs in O(nlogn) time.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Are you sure it's linear complexity? Arrays.sort is merge sort, and that runs in O(nlogn) time.



That's why I said: IT'S LINEAR BUT REQUIRES THE INPUT ARRAYS TO BE SORTED.

The input requirement is two sorted arrays. Then the algorithm calculates the sorted intersection set in linear time.
+Pie Number of slices to send: Send
Agreed on that point.
+Pie Number of slices to send: Send
Embla, could you please use other means of emphasizing pieces of text? Quoted, bold or italic are much nicer to read than caps.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Agreed on that point.



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.
+Pie Number of slices to send: Send
Yes, that clarifies the point Thank you.
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3070 times.
Similar Threads
about random.next int();
Empty strings
Need suggestion on implementation
If in a While not working
Compare the values in 2 array.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:56:01.