This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Linear comparison algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Linear comparison algorithm" Watch "Linear comparison algorithm" New topic
Author

Linear comparison algorithm

Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
I'm assuming you posted before I submitted my last revision. I also changed the Object arrays to int arrays in the driver. I am now getting the correct elements just not enough comparisons.
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
Finally figured it out.... But one last thing. Any ideas on how I can include duplicate elements? I've sorted the arrays and managed to find all common elements, but I want multiple values of a common element.
Such as, 28, which appears in only two sets but should be printed twice in the common set.






Ouput:

The following elements were common among all arrays:

[[1, 2, 3, 4, 5, 16, 22, 23, 25, 28]]

Additionally, there were a total of 40 comparisons made between the arrays.

[Ljava.lang.Comparable;@3ee284 <<<<<<---- not sure why this though??
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
"hashset"....And from your suggestions, though I had no idea how to implement it at first. (I'm referring to the comments I left in the code).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32668
    
    4
Ian Mcloud wrote: . . .
// why do I keep getting [Ljava.lang.Comparable;@3ee284 . . .

[Ljava.lang.Comparable;@3ee284 <<<<<<---- not sure why this though??
Because the only method which an array overrides is clone(). You are using Object#toString inherited unchanged.
Ian Mcloud
Ranch Hand

Joined: Oct 04, 2012
Posts: 76
I see. So cast it somehow?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 4749
    
    7

Ian Mcloud wrote:Finally figured it out....

Well done. And I notice you've now included a HashSet, in which case you might be interested in its retailAll() method - it'll reduce your code even more.

But one last thing. Any ideas on how I can include duplicate elements? I've sorted the arrays and managed to find all common elements, but I want multiple values of a common element. Such as, 28, which appears in only two sets but should be printed twice in the common set.

Hunh? I thought you were only interested in elements that were common to all arrays. When I was talking about duplicates, I meant including two copies of an element in your result only if ALL arrays have at least two of them.
And the only way I know to do that is:
1. Count occurrences of each distinct value in each array.
2. Eliminate values where any of the counts is 0;
3. If the minimum count is > 1, include that number of copies in the output.
And for that, you will almost certainly need a Map (unless you want to write a lot of unnecessary code).

If that's not what you want, I think you'll have to describe your requirements a bit more clearly.

I see. So cast it somehow?

No, no, no. In fact, your program has far too many casts in it already (you could actually write it without a single one). Casts are evil, and to be avoided at all costs.

Winston

PS: Please remember what I said about DontWriteLongLines. I've broken yours up again, but this is the last time.

Isn't it funny how there's always time and money enough to do it WRONG?
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Linear comparison algorithm
 
Similar Threads
Binary search???
calling submit from a thread pool in the same pool it belongs to
Cannot throw exception
Question about finding common integer from two integer array
Sorting Asciibetically