0

I have the two string array name str_arr1[],str_arr2[]. The two array having same or different value.

str_arr1[] = { "one", "two", "three","four","Doctor","Engineer","Driver" };
str_arr2[] = { "one", "Doctor","Engineer" };

Usually str_arr1[] having more record compare to str_arr2[]. I want to check the str_arr1[] having str_arr2[]. If it's true means return true. Otherwise return false.

7 Answers 7

3
bool contains = !str_arr2.Except(str_arr1).Any();
Sign up to request clarification or add additional context in comments.

Comments

3
function compareArrays(arr1, arr2) {
  if (arr1.length != arr2.length) {
    return false;
  }

  return arr1.sort().join() == arr2.sort().join();
}
var arr1 = ["one", "two", "three","four","Doctor","Engineer","Driver" ];
var arr2 = ["one", "Doctor","Engineer"];

    console.log(compareArrays(arr1,arr2));

Comments

2

Please use this

static bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1,a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}

Comments

1
bool result=str_arr2.Any(x=>!str_arr1.Contains(x));

Comments

0
var str_arr1 = new string[] { "one", "two", "three", "four", "Doctor", "Engineer", "Driver" };
        var str_arr2 = new string[] { "one", "Doctor", "Engineer" };

        return str_arr1.Intersect(str_arr2).Count() == str_arr2.Length;

Comments

0
var inter = str_arr1.Intersect( str_arr2);

foreach (var s in inter)
{
    Console.WriteLine("present" + s); // or you can return somthin
}

Comments

0

You did not cleared the conditions. I am combining all the conditions in this.

 string[] str_arr1 = new string[] { "one", "two", "three", "four", "Doctor", "Engineer", "Driver" };
 string[] str_arr2 = new string[] { "one", "aaaa", "yyy" };

 bool contains = !str_arr2.Except(str_arr1).Any(); // this will show true only if all the items of str_arr2 are in str_arr1 

 bool result = str_arr2.Any(x => !str_arr1.Contains(x));// this will show true if any of the items of str_arr2 are in str_arr1 

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.