Count pairs in list of integers such that their addition is equal to the input value Given a List&lt;int&gt;, the problem I am trying to solve is:
Find the number of unique pairs in List&lt;int&gt; such that their addition is exactly equal to the input value. 

The bottleneck is a nested for() loop that I have used to go through the list

// *** OPTIMIZE THIS LOOP ***
for (int i = 0; i &lt; intList.Count - 1; i++)
{
    for (int j = i + 1; j &lt; intList.Count; j++) 
    {
        if ((intList[i] + intList[j] == totalValue) &amp;&amp; 
              IsPairUnique(intList[i], intList[j]) == true)
        {
            nCombinations++;
        }
    }
}


How can this be made better for performance? I understand 'better' is a subjective word and has no real meaning as such. For a List&lt;int&gt; of size 50000 the code takes about 18 seconds on one of my VM. For 500000 it's way too worst. So, here by 'better' I mean 'faster'. Clearly this problem deserves much less than 18 seconds to solve in my opinion. With 1 Parallel.For() I have managed to get the loop time to 10-11 seconds, but I have a feeling that this whole algorithm needs a fresh set of eyes to look at. 

Parallel.For(0, intList.Count - 1,
        i =&gt;        
        {            
            for (int j = i + 1; j &lt; intList.Count; j++)
            {
                if ((intList[i] + intList[j] == totalValue) &amp;&amp; IsPairUnique(intList[i], intList[j]) == true)
                {         
                    nCombinations++;
                }
            }
        });


How can I speed this up? 
Full code from my console application is as below: 

class TestClass
{
    static List&lt;int&gt; compareList = new List&lt;int&gt;();
// GetPossibleCombination method.
// This method finds out the unique number of possible combinations where 
// addition of any 2 values from the list is exactly equal to 'totalValue'
static int GetPossibleCombinations(List&lt;int&gt; intList, long totalValue)
{
    // handle edge conditions
    if (intList == null || 
        intList.Count == 0 || 
        intList.Count &gt; 500000 || 
        totalValue &gt; 5000000000)
        return 0;

    compareList.Clear();
    int nCombinations = 0;


    // *** OPTIMIZE THIS LOOP ***
    for (int i = 0; i &lt; intList.Count - 1; i++)
    {
        for (int j = i + 1; j &lt; intList.Count; j++) // start from this element onwards
        {
            if ((intList[i] + intList[j] == totalValue) &amp;&amp; IsPairUnique(intList[i], intList[j]) == true)
            {
                nCombinations++;
            }
        }
    }

    return nCombinations;
}

// This method creates a list of possible values we have 
static bool IsPairUnique(int v1, int v2)
{
    if (compareList.Contains(v1 * 10 + v2) == true || compareList.Contains(v2 * 10 + v1) == true)
        return false;
    else
    {
        // else add a new one
        compareList.Add(v1 * 10 + v2);
        compareList.Add(v2 * 10 + v1);
    }

    return true;
}

static void Main(string[] args)
{        
    int intListSize = 50000; // Optimize it for numbers upto 500,000
    long totalValue = 5000;

    List&lt;int&gt; intList = new List&lt;int&gt;();
    Random r = new Random();

    for (int i = 0; i &lt; intListSize; i++)
    {            
        intList.Add(r.Next(0, 10000)); // populate random values.
    }

    Stopwatch sw = new Stopwatch();
    sw.Start();

    // Find the number of unique pairs in 'intList' such that 
    // their addition is exactly equal to 'totalValue'
    int res = GetPossibleCombinations(intList, totalValue);

    sw.Stop();
    Console.WriteLine(sw.Elapsed.ToString());
    }
}
The solution has a quadratic time complexity. If it solves the 50000-strong list in 18 seconds, I'd expect about 1800 seconds (aka 30 minutes) for a 500000 one. Notice that the problem is aggravated by the way you are looking for duplicates (and IsPairUnique doesn't look correct; it is prone to false positives).

There are few standard ways to bring the complexity down.


Sort the list. Arrange two iterators, one at the beginning, another at the end.

Now add the pointed elements. If the sum is less than the target, advance the left one. If it is greater, move the right one toward the beginning. If it is a target, record it, and move both. In any case, when moving an iterator, skip duplicates. Rinse and repeat until they met.
If you have enough extra memory, put them in a set. For each element in a set check if its complement is also there.