Avoid using global variable in Java 8 stream reduce method
<p>I am trying to use Java 8 to rewrite the implementation of <a href="http://www.geeksforgeeks.org/majority-element/" rel="noreferrer">Moore’s Voting Algorithm</a> to find the Majority Element in an array.</p>

<p>The Java 7 implementation will be something like this:</p>

<pre><code>public int findCandidate(int[] nums) {

    int maj_index = 0, count = 1;
    for(int i=1; i&lt;nums.length;i++){
        if(count==0){
            count++;
            maj_index=i;
        }else if(nums[maj_index]==nums[i]){
            count++;
        } else {
            count--;
        }
    }
    return nums[maj_index];
}
</code></pre>

<p>The method I can think of is using stream reduce to get the final result</p>

<pre><code>public int findCandidate(int[] nums) {
    int count = 1;
    Arrays
            .asList(nums)
            .stream()
            .reduce(0, (result, cur) -&gt; {
                if (count == 0) {
                    result = cur;
                    count++;
                } else if (result == cur){
                    count++;
                } else {
                    count --;
                }
            });
    return result;
}
</code></pre>

<p>But this method have compile error, besides, it also break the functional purist, I encounter this situation many times, so what is the best way to deal with the global variable inside the lambda expression. </p>