ASP.NET - How to uppend - Asked By msakt on 07-Jun-12 08:33 AM

S = 1,2,3,4;

s1 = 1,5,2,6,7;


now i wnt to append  s2 = 1,2,3,4,5,6,7;
Chintan Vaghela replied to msakt on 07-Jun-12 09:01 AM

Hi Frndz,

 

Functionality ::  Concate two string and Remove Duplicate

 

Take to string into array. And split with comma seperator.

 

After then using linq to remvoe duplicate

 

LINQ Query ::

 

 

 

string S = "1,2,3,4";

        string S1 = "2,4,5,6";

 

        string[] arrstr = S.Split(',');

 

        string[] arrstr1 = S1.Split(',');

 

 

        var result = arrstr.Concat(arrstr1).GroupBy(x => x.ToString()).Select(x => x.First()).ToList();

 

 

        string S2 = "";

        foreach (var item in result)

        {

          S2 = S2 + "," + result;

        }

        S2 = S2.TrimStart(',');

 

 

Hope this helpful!

Thanks

 

Jitendra Faye replied to msakt on 07-Jun-12 10:10 AM
Use this working code-

        string s1  = "1,2,3,4";
        string s2 = "1,5,2,6,7";

      
  s1 = s1 + "," + s2;

        string[] arr = s1.Split(',');

        string result = "";
        foreach (string s in arr)
        {
          if (!result.Contains(s))
          {
            result = result + s + ",";
          }

        }
        result = result.Substring(0,result.Length - 1); 

output-

1,2,3,4,5,6,7

Try this and let me know.

TSN ... replied to msakt on 08-Jun-12 01:20 AM
   
HI.....

Just use simple Unoin , it will take care of the duplicates by itself

 int[] arr1 = { 1,2,4,6};
    int[] arr2 = { 3,2,6,5 };

    var arr3 = arr1.Union(arr2);

    foreach (int n in arr3)
    {
      Console.WriteLine(n.ToString());
    }