Mike Simmons wrote:The XOR trick is cool and super fast, but only really works for this very specific problem. What if there were two elements that only occur once?
Great point!
I thought the same problem will be with the Set solution I posted above, but I am unable to get it to fail on input like
6
3 1 3 1 2 3
OR
6
2 3 1 3 1 3
OR
6
3 1 3 1 3 2
I expect it to return 3 (incorrect value) but it returns 2 (correct value).
Mike Simmons wrote:
I do think it's pretty useful to know how to get a frequency count of items in an array, collection, or stream. Collectors.groupingBy() is definitely useful - but not a complete solution itself. The standard library does provide several other methods you can combine for a fairly concise solution:
I am trying to understand
Does the classifier Function.identity() work only because the elements are Integers with only one attribute (their value)
- if they were objects with other attributes then we would have to supply the method Foo::getAttribute ?
Trying to step through it in the
IDE, This is really formidable code!