简体   繁体   中英

Finding if a target number is the sum of two numbers in an array via LINQ and get the and Indices

Hello I am new to Linq , I found this thread which explain 90% of what I need https://stackoverflow.com/questions/2331882?tab=newest#tab-top , thanks "pdr"

but what I need is to get the Indices too , here is my modification I get the index of the first number but I don't know how to get the index of the second number

         int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        var result = from item in numbers.Select((n1, idx) =>
             new { n1,idx, shortList = numbers.Take(idx) })
                     from n2 in item.shortList
                     where item.n1 + n2 == 7
                     select new { nx1 = item.n1,index1=item.idx, nx2=n2  };

SelectMany is what you need ...

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int target = 7;

var query = numbers
              .SelectMany((num1,j) => numbers.Select((num2,i) => new {n1=num1, n2=num2, i=i, j=j}))
              .Where(x => x.n1 + x.n2 == target && x.i < x.j);

foreach (var x in query)        
   Console.WriteLine(x.n1 + " and " + x.n2 + " occur at " + x.i + "," + x.j );

This will give you a pair of items, each of which containing a value from the array and its index. The pairs are limited to where the sums of the values equal a target. The && on the where clause eliminates duplicates and self-matching (applicable for even targets).

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

int target = 7;

var query = from item1 in numbers.Select((number, index) => new { Number = number, Index = index })
            from item2 in numbers.Select((number, index) => new { Number = number, Index = index })
            where item1.Number + item2.Number == target
            && item1.Index < item2.Index
            select new { Item1 = item1, Item2 = item2 };

foreach (var itemPair in query)
{
    Console.WriteLine("{0}:{1}\t{2}:{3}",
        itemPair.Item1.Index,
        itemPair.Item1.Number,
        itemPair.Item2.Index,
        itemPair.Item2.Number);
}

If it's definitely over an array then you can just do:

var result = from index1 in Enumerable.Range(0, numbers.Length)
             from index2 in Enumerable.Range(index1 + 1,
                                             numbers.Length - index - 1)
             where numbers[index1] + numbers[index2] == targetNumber
             select new { index1, index2, 
                          value1 = numbers[index1], value2 = numbers[index2] };

Otherwise, you can use the Select form that includes the index twice:

var result = from pair1 in numbers.Select((value, index) => new { value, index})
             from pair2 in numbers.Skip(pair1.index + 1)
                                  .Select((value, index) => 
                                   new { value, index = index - pair2.index - 1})
             where pair1.value + pair2.value == targetNumber
             select new { index1 = pair1.index, index2 = pair2.index,
                          value1 = pair1.value, value2 = pair2.value };

Both of these are really ugly though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM