I want to create a method to find the index of two numbers whose sum is target.
So, I have created this method:
public static int[] TwoSum(int[] nums, int target)
{
for (var i = 0; i < nums.Length; i++)
{
for (var j = 0; j < nums.Length; j++)
{
if (j == i) continue;
if (nums[i] + nums[j] == target)
{
return new[] { i, j };
}
}
}
}
Which works fine. However, I am trying to learn myself some LINQ and can't figure it out. I looked at various examples, but I always end up stuck because I am using the same array twice. So I don't know what to select and how to access it twice while also making sure it doesn't go through the same indexes twice.
Any help getting a LINQ from the above loops would be appreciated.
Sample data:
var nums = new [] { 2, 7, 11, 15 };
var target = 9;