简体   繁体   中英

Check if an array contains all the values greater than another array at the same index

For any two given arrays, is there any simple way of checking if all the elements of one array are greater than that of the other array at the same index

For eg:

Arr1 = [1,2,3] and Arr2 = [2,3,4] returns true

while

Arr1 = [1,2,3] and Arr2 = [2,1,4] returns false

您可以使用LINQ:通过索引进行Zip链接,并且All计算一个谓词,并在第一个不匹配项上返回false

bool allGreater = Arr1.Zip(Arr2, (i1, i2) => i2 > i1).All(secondGreater => secondGreater);
Enumerable.Range(0, Arr2.Length).All(i => Arr1[i] < Arr2[i])

Or, if the arrays can be different lengths:

Enumerable.Range(0, Math.Min(Arr1.Length, Arr2.Length))
    .All(i => Arr1[i] < Arr2[i])

you can use for loop

function is2ArrayGreater(Arr1, Arr2){
  if(Arr1.length == Arr2.length){
    var isEqual = true;
    for(var i = 0; i< Arr1.length; i++){
      if(Arr1[i] >= Arr2[i]){
        isEqual = false;
        break;
      }
    }
    alert('result is: ' + isEqual);
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM