1

I have a 2 dimensional array int[][] data and i want to see if the value contains the value 45 for example

I created this line of code

bool contains = data.Where(x => x.Contains(45)).ToArray().Count() != 0 ? true : false;

but it looks like there is unnecessary code and I am sure that there is an easier or more efficient way to do this

6
  • 1
    Well the .ToArray().Count() != 0 can be replaced by .Any(), but more importantly we can't guess what does make you happy. Please read How to Ask and try to edit this into an actually answerable question, and share your research. Commented Aug 8, 2016 at 8:44
  • @CodeCaster well using toarray,count and then another function or check to verify if it isnt 0 seems a but long obviously this is what makes me unhappy :/ dont know how this is confusing to you Commented Aug 8, 2016 at 8:47
  • 1
    What is confusing to me is what you expect of this question. "Here is some code that makes me unhappy" isn't really answerable. Do you want people to post random snippets of code that essentially does the same as your code, but which makes you "happy"? Why don't you start by listing the things that make you happy then? Did you try reading How to Ask and sharing your research by editing your question? Did you find How to check if multidimensional array row contains non-Zero value, for example? Commented Aug 8, 2016 at 8:50
  • Event though the question was solved, you do realize that you can remove the { ? true : false } in your sample and it would be exactly the same right? no need for it. Commented Aug 8, 2016 at 8:50
  • @CodeCaster ok i removed the word unhappy. are you happy? Commented Aug 8, 2016 at 8:52

3 Answers 3

3

You could use Any linq extension.

bool exists = data.SelectMany(x=>x).Any(x=>x == 45);

Or

bool exists = data.Any(x=>x.Any(s=>s == 45));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this is what i am looking for.
3

Array.IndexOf is optimised for integer arrays, so if your inner arrays are long and you care about speeding it up that much it might be worth doing it like this:

bool exists = data.Any(a => Array.IndexOf(a, 45) != -1);

2 Comments

An int[] is an ICollection<int>. Enumerable.Contains<T> calls the ICollection<T>.Contains method if the source is an ICollection<T>. So in the version data.Any(a => a.Contains(45)) the Contains method of array is used, and this is as well optimized as IndexOf. But good point that this will be a little bit better than the SelectMany version. Though I think it's only a micro-optimization even for very large arrays (but I did not measure it).
Thank you this was noted
2

There are two issues in your code:

  1. data is not an array of int, it is an array int[]
  2. the ternary operator takes a bool condition and returns a value based on the bool value. So x ? true : false is the same as x. There is no need for the operator.

So what (I assume) you want is not to check if data contains 45, but if any of the arrays in data contains 45. So you either flatten the jagged array into on enumeration using SelectMany or concat two Any calls:

bool contains = data.SelectMany(d => d).Contains(45);

or

bool contains = data.Any(d => d.Contains(45);

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.