0

I want to merge all data in two arraylist in c#. Some data in arraylist got same with another arraylist. i don't want duplicate data when merge.

ArrayList1   ArrayList2
    1             1
    2             2
    3             4
                  5

I've try the coding below. but the result not as i expected:

for(int i = 0; i<arrayList2;i++)
{
  for(int j = 0; j<arrayList1;j++)
  {
     if(arraylist1[i] == arraylist2[j])
     {
        newArraylist.add(arraylist[i]+"-same");
     }
     else
     {
        newArraylist.add(arraylist[i]+"-from arrayList2");
     }
  }
}

The result from code above is :

newArrayList
   1 - same
   2 - same
   4 - from arraylist2
   5 - from arraylist2

the result that i want :

 newArrayList
       1 - same
       2 - same
       3 - from arraylist1
       4 - from arraylist2
       5 - from arraylist2
7
  • 5
    Given that you're using C# 2, is there any reason you're not using List<T>? Commented Jul 5, 2012 at 9:06
  • Erm, how can you expect an output of from arraylist1 when that's not even in your code snippet? Commented Jul 5, 2012 at 9:08
  • @JonSkeet i'm using the ArrayList because the old developer leave me this software with ArrayList.. owh why.-_-" Commented Jul 5, 2012 at 9:08
  • @Shevek because i don't know how... please help. Commented Jul 5, 2012 at 9:09
  • Get generic, abandon arraylist. Commented Jul 5, 2012 at 9:09

6 Answers 6

3

The most simple and easiest way is using LINQ

var collection1 = new ArrayList() { 1, 2, 3 };
var collection2 = new ArrayList() { 1, 2, 4, 5 };
var mergedList = new ArrayList();
mergedList.AddRange(collection1);
mergedList.AddRange(collection2.ToArray().Where(item => !collection1.ToArray().Contains(item)).ToList());
Console.WriteLine(String.Join(", ", mergedList.ToArray()));
Console.ReadLine();
Sign up to request clarification or add additional context in comments.

1 Comment

collection1 doesn't need to be converted to an array to check Contains(item).
2

How about this:

var arrayList1 = new ArrayList {1, 2, 3};
var arrayList2 = new ArrayList {1, 2, 4, 5};
var newList = new ArrayList();

foreach (var item in arrayList1)
{
    if(arrayList2.Contains(item))
    {
        newList.Add(string.Format("{0} - same", item));
        arrayList2.Remove(item);
    }
    else
    {
        newList.Add(string.Format("{0} - from arrayList1", item));
    }
}

foreach(var item in arrayList2)
{
    newList.Add(string.Format("{0} - from arrayList2", item));
}

This will give you this result:

1 - same
2 - same
3 - from arraylist1
4 - from arraylist2
5 - from arraylist2

Comments

1

Loop over the items and seee it it is contained in the first array.

foreach (int item in arrayList2)
{
    if (!arrayList1.Contains(item))
        arrayList1.Add(item);
}

Comments

0

You're comparing based on index, which won't work. ArrayList1[2] contains 3, while ArrayList1[2] contains 4.

You'll have to use .Contains() to check whether the number is present in the other list.

Comments

0

Code

var collection1 = new ArrayList() { 1, 2, 3 };
var collection2 = new ArrayList() { 1, 2, 4, 5 };

var merge = collection1.ToArray().Union(collection2.ToArray()).Distinct();
var result = new List<String>();
foreach (var val in merge)
{
    var newString = string.Empty;
    if (collection1.Contains(val) && collection2.Contains(val))
        newString = val.ToString() + " - same";
    else if (collection1.Contains(val))
        newString = val.ToString() + " - from list 1";
    else
         newString = val.ToString() + " - from list 2";
    Console.WriteLine(newString);
    result.Add(newString);
}

Produces output

1 - same
2 - same
3 - from list 1
4 - from list 2
5 - from list 2

2 Comments

Union is a 3.5 feature. Not available in 2.0
My bad. Well, own Union() method can be written easily enough :)
0

The cleanest option would be using Union (LINQ). To find out how it works check this: http://www.dotnetperls.com/union

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.