locked
How to compare two arraylists RRS feed

  • Question

  •  

         I want to compare two arraylists and find out the different values in those arraylists.

     

     

    Thursday, November 1, 2007 8:01 AM

Answers

  •  

    Code Block

    using System;

    using System.Collections;

    using System.Collections.Generic;

     

    public class MyClass

    {

     

    public static void Main()

    {

    ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});

    ArrayList list2 = new ArrayList(new int[] {1,2,8});

    for (int i=0; i<list1.Count && i<list2.Count; i++)

    {

    if (!Object.Equals(list1[i], list2[i]))

    Console.WriteLine("Different value at index {0}.", i);

    }

    if (list1.Count > list2.Count)

    Console.WriteLine("list1 has more elements than list2.");

    if (list2.Count > list1.Count)

    Console.WriteLine("list2 has more elements than list1.");

    Console.ReadLine();

    }

     

    }

     

     

    Thursday, November 1, 2007 10:40 AM

All replies

  •  

    Code Block

    using System;

    using System.Collections;

    using System.Collections.Generic;

     

    public class MyClass

    {

     

    public static void Main()

    {

    ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});

    ArrayList list2 = new ArrayList(new int[] {1,2,8});

    for (int i=0; i<list1.Count && i<list2.Count; i++)

    {

    if (!Object.Equals(list1[i], list2[i]))

    Console.WriteLine("Different value at index {0}.", i);

    }

    if (list1.Count > list2.Count)

    Console.WriteLine("list1 has more elements than list2.");

    if (list2.Count > list1.Count)

    Console.WriteLine("list2 has more elements than list1.");

    Console.ReadLine();

    }

     

    }

     

     

    Thursday, November 1, 2007 10:40 AM
  • Hi,

    Just try this code.

     

    ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});

    ArrayList list2 = new ArrayList(new int[] {1,2,8});

    int Count = list1.Count;

    Console.WriteLine("Different element at list 1 ");

    for (int j=0; j<Count; j++)

    {

    if (!list2.Contains(list1[j]))

    Console.WriteLine("Different element at location :"+j+ " Value:"+list1[j]);

    }

    Count = list2.Count;

    Console.WriteLine("Different element at list 2 ");

    for (int j=0; j<Count; j++)

    {

    if (!list1.Contains(list2[j]))

    Console.WriteLine("Different element at location :"+j+ " Value:"+list1[j]);

    }

     

     

    Regards,

    Manju Sandhu

    Thursday, November 1, 2007 12:26 PM
  •  

    Manju I think your would throw an exception if the lists had differing counts.

     

    If list 1 had 30 and list 2 had 35

     

    Then when we get to list2 31st element you would get an outofbounds excetion here

     

    j=30; (31,32,33,34)

    (!list1.Contains(list2[j]))

     

    List1 only has 30 elements.

    Thursday, November 1, 2007 3:16 PM
  • 1. Sort both arrays.

    2. Define two counter variables (one for each of your two arrays).

    3. Start to advance both counters on both arrays in an endless loop.

    4. At each iteration:

    • If values (referenced by counters) in both arrays are different - print both values and advance counter for an array with the smaller value.
    • If values are the same - skip both and advance both counters.
    • If you reached the end of 1 of your arrays - print value from the 2nd array only and advance its counter.
    • Break when you reach the end of longer array.

    It gives you complexity in n*log(n) bounds.

     

    Regards,

           Alex Pinsker

           http://alexpinsker.blogspot.com

     

    p.s.

    I'm in too lazy mood to write it in C#, sorry ;o).

     

    Thursday, November 1, 2007 4:32 PM