0

I'm a student and I was wondering what the most efficient way is to check if a certain value is present in a array.

My second attempt:

string value = "pow";
string[] array = new string[] { "pong", "ping", "pow" };
bool valueIsInArray = false;
foreach(var s in array) if (s == value) valueIsInArray = true;
if (valueIsInArray)
{
    // code here
}

I've researched and found if I were to use LINQ the code would look like this:

string value = "oink"; // value given to the method
string[] array = new string[] { "oink", "oink", "baboinkadoink" };
if (array.Contains(value))
{
    //code here
}

The question is if using LINQ in anyway negatively impacts the speed or consistency of the code, and if there is an even better way to go about doing this?

  • using `LINQ` is just fine... your first method actually quite poor because it will iterate all the items though you have found the matching item. You should at least using `break` for your first method. – Ian May 10 '17 at 09:58
  • The difference is a question of *microseconds*; put the version which is more *readable* – Dmitry Bychenko May 10 '17 at 09:59
  • As stated I'm just a student at the time, and would like to know if using LINQ does some unnecessary steps that would slow down/cripple the consistency of the code. And Indeed I guess I could add a "break" upon finding a match. – Dwight VdV May 10 '17 at 10:02
  • If you are doing this once, performance probably doesn't matter. If you are checking lots of things, then use a `HashSet` as it is a constant-time check. – Scroog1 May 10 '17 at 10:13

4 Answers4

1

As a commenter said, LiNQ won't really trouble you here. The difference is microscopic (even on larger collections). However, if you must use an alternative, use IndexOf. See: https://msdn.microsoft.com/en-us/library/system.array.indexof(v=vs.110).aspx

Example:

string value = "oink"; // value given to the method
string[] array = new string[] { "oink", "oink", "baboinkadoink" };
if (Array.IndexOf(array, value) > -1)
{
    //code here
}

Although I'm not sure what Contains ends up doing underwater, but they probably make a call to IndexOf aswell.

nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • Yea thanks! Your example is also a great way I didn't think of yet! :) I might just use the contains method to make it more readable to me. – Dwight VdV May 10 '17 at 10:14
1

Use linq Any(), The enumeration of source is stopped as soon as the result can be determined.

string value = "pow";
string[] array = new string[] { "pong", "ping", "pow" };
bool isValuePresent =  array.Any(x => x == value);

https://msdn.microsoft.com/en-us/library/bb534972(v=vs.110).aspx

Prasanth V J
  • 1,126
  • 14
  • 32
  • Just did [research](http://stackoverflow.com/questions/23526773/what-is-the-difference-between-contains-and-any-in-linq) and decided to not use any since it is not needed at this current moment to complicate it to that extent. I think the contains will be compacter for the time being but I learned anyways so thanks :) – Dwight VdV May 10 '17 at 10:10
0

You shoud have to iterate through the entire array for checking the value. Either Linq or Conventional looping methods. Or you can use the

Array.Find()

also for the same. Better to go with the Linq and make the code is more simpler.

Happy coding

BOBIN JOSEPH
  • 1,012
  • 8
  • 25
0

Willy-nilly you have to scan the array up to the first match (or entire array if there's no match); you can either put foreach loop:

 bool valueIsInArray = false;

 foreach (var item in array)
   if (item == value) {
     valueIsInArray = true;

     break; 
   }

use for one:

 bool valueIsInArray = false;

 foreach (int i = 0; i < array.Length; ++i)
   if (array[i] == value) {
     valueIsInArray = true;

     break; 
   }

Try Array class:

bool valueIsInArray = array.Contains(value);

Implement the code with a help of Linq:

bool valueIsInArray = array.Any(item => item == value);

The difference of these methods is a question of microseconds (if any); that's why put the version which is the most readable for you. My own choice is array.Contains(value) - let the system work for you and hide unwanted details (e.g. break in the loop)

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Wow thanks! Yeah I think I'll just use the contains option; it looks much more readable to me. – Dwight VdV May 10 '17 at 10:13
  • @Dwight VdV: it's you has who is a *master*, and the system should work for you (= check for contains), not you for the system (= scan each item in the `array` if you've found `value` in the process...) – Dmitry Bychenko May 10 '17 at 10:15