Cags
Contributor
Say I have the following...
I need to populate arr3 with values that are in both arr1 and arr2. I'm currently doing it with the following, does anyone know of a better way of achieving this?
Just to clarify arr3 should now contain 2, 5 and 7. As I've been typing this post I've realised that in this example arr1 is larger than arr2 so it would perhaps be prudent to loop through the smaller arr2 so I might add an if statement if nobody else can think of a better solution.
C#:
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
ArrayList arr3 = new ArrayList();
arr1.AddRange(new int[] { 1,2,3,4,5,6,7,8,9 });
arr2.AddRange(new int[] { 2, 5, 7 });
I need to populate arr3 with values that are in both arr1 and arr2. I'm currently doing it with the following, does anyone know of a better way of achieving this?
C#:
for(int i = 0; i < arr1.Count; i++)
if(arr2.Contains(arr1[i]))
arr3.Add(arr1[i]);
Just to clarify arr3 should now contain 2, 5 and 7. As I've been typing this post I've realised that in this example arr1 is larger than arr2 so it would perhaps be prudent to loop through the smaller arr2 so I might add an if statement if nobody else can think of a better solution.