-3

I want to compare 2 arrays and check if they have 1 or more equal elements then I want to print this element like:

array1[a ,b ,c , d, e]
array2[e, r, t , a, v]

Console.println["a"]
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64

1 Answers1

1

You could use this

var commonElements = array1.Intersect(array2).ToArray();

If commonElements.Length>0 the arrays array1 and array2 have at least one common element.

Then you can print the common elements by just iterating through the items of the commonElements array.

foreach(var commonElement in commonElements)
    Console.WriteLine(commonElement);
Christos
  • 53,228
  • 8
  • 76
  • 108
  • but is there also a way too see what element they have in common>? – Jordy Huijgens Jan 07 '15 at 13:16
  • is there also a way to see what line this element is ? – Jordy Huijgens Jan 07 '15 at 14:31
  • @JordyHuijgens saying what line, what do you mean? You have an array of elements. If you want, you can get the index of the element in the array. – Christos Jan 07 '15 at 14:33
  • i mean like the array has like 10 elements and i want to know wich element it is f its the 1st 2nd 3rd ,, im sorry im really new with arrays – Jordy Huijgens Jan 07 '15 at 14:41
  • if this is my array: [0]: dog [1]: lazy [2]: the [3]: over [4]: jumps [5]: fox [6]: brown [7]: quick [8]: The And i want to see wich string belongs to number 7 how can i do that? – Jordy Huijgens Jan 07 '15 at 15:14
  • You simply type this `array[7]`. – Christos Jan 07 '15 at 15:15
  • thats what i want but then backwards like array[quick]? i wanna do that with ther commonelements – Jordy Huijgens Jan 07 '15 at 15:24
  • @JordyHuijgens I am sorry, but I can't get you. `commonElements` is an array. Do you want to see what is stored in position with index 2? You just write this `commonElements[2]`. If there isn't any element in this position, the array's length is 2, it has two elements one in position 0 and one in position 1, then you will get an exception. – Christos Jan 07 '15 at 15:31
  • i want to know, how i can get the number of the line where my string is at i know the string but i dont know the line ,, u showed me Array[7] ,, but what i want to know is Array[brown] for example ,, the output should be 6 becouse when u look at the array : [0]: dog [1]: lazy [2]: the [3]: over [4]: jumps [5]: fox [6]: brown [7]: quick [8] 6 is brown – Jordy Huijgens Jan 07 '15 at 15:34
  • Ah, Then that you want is the array's method called `IndexOf`. `int index = Array.IndexOf(commonElements,"dog");` – Christos Jan 07 '15 at 15:36