3 sum algorithm - Trying Recently I started looking and refreshing about algorithms and decided to try to do the 3 sum algorithm. I did the code below:

public static int[] tripletSum(int[] A, int sum) {


    HashMap&lt;Integer, Boolean&gt; data = new HashMap&lt;&gt;();

    for (int i = 0; i &lt; A.length; i++) {
        data.put(A[i], true);
    }

    int i = 0;
    int j = 1;

    while (i &lt; A.length &amp;&amp; j &lt; A.length) {

        Integer a = A[i];

        Integer b = A[j];

        Integer delta = sum - (a + b);

        if (data.containsKey(delta) &amp;&amp; a != delta &amp;&amp; b != delta &amp;&amp; (a + b + delta) == sum) {
            int[] result = {a, b, delta};
            return result;

        }
        i++;
        j++;
    }
    return null;
}


I'd like opinions about if it is good logic, time complexity, improvements. Any tips and comments will be great. 

The piece of code below:

if (data.containsKey(remainder) &amp;&amp; a != remainder &amp;&amp; b != remainder &amp;&amp; (a + b + remainder) == sum) {


is included to avoid to repeat values in a and b, and to guarantee the sum result as expected.

Thanks
There is no reason to use HashMap, you are not using the bools anywhere. So I think You could just use HashSet. Which you don't need to feed in foreach, instead simply pass the integers as constructor parameter.

var set = new HashSet(A);


You don't have to compare both i and j

i &lt; A.length &amp;&amp; j &lt; A.length


if j is always one larger then i, just compare with j.

This condition is always true:

(a + b + delta) == sum


since that is the way it was obtained in the first place:

Integer delta = sum - (a + b);


This:

int[] result = {a, b, delta};
return result;


could be just

return {a, b, delta};


You are returning null where you claim to return int[], not sure if that's correct.

Anyway I think your implementation does not work as intended, because it only looks for sums of two consecutive and third anywhere. It fails for example on [5,1,2,1,8] looking for 15=5+2+8.