How join two int arrays based on some condition in C#

3 Answers

0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 3, 4, 5, 6, 9, 6 };

            // If arr1 contains elements that exist in arr2 + 1 
            // 1+1=2 not exist 
            // 2+1=3 exist join  2
            // 3+1=4 exist join  3
            // 4+1=5 exist join  4
            // 5+1=6 exist twice join 5 5
            var result = arr1.Join<int, int, int, int>(arr2,
                x => x + 1,
                y => y, 
                (x, y) => x);

            foreach (var n in result)
                Console.WriteLine(n);
        }
    }
}


/*
run:

2
3
4
5
5

*/

 



answered Feb 23, 2017 by avibootz
0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 3, 4, 5, 6, 9, 6 };

            // If arr1 contains elements that exist in arr2 + 1 
            // 1+2=3 exist join  1
            // 2+2=4 exist join  2
            // 3+2=5 exist join  3
            // 4+2=6 exist twice join 4 4 
            // 5+2=7 not exist 
            var result = arr1.Join<int, int, int, int>(arr2,
                x => x + 2,
                y => y, 
                (x, y) => x);

            foreach (var n in result)
                Console.WriteLine(n);
        }
    }
}


/*
run:

1
2
3
4
4

*/

 



answered Feb 23, 2017 by avibootz
0 votes
using System;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = { 1, 2, 3, 4, 5 };
            int[] arr2 = { 3, 4, 5, 6, 9, 6 };

            // If arr1 contains elements that exist in arr2 + 1 
            // 1+3=4 exist join  1
            // 2+3=5 exist join  2
            // 3+3=6 exist twice join 3 3
            // 4+3=7 not exist 
            // 5+3=8 not exist 
            var result = from v in arr1
                         join x in arr2 on (v + 3) equals x
                         select v;

            foreach (var n in result)
                Console.WriteLine(n);
        }
    }
}


/*
run:

1
2
3
3

*/

 



answered Feb 23, 2017 by avibootz
...