-3

i'm having two arrays of booleans:

arr1 = [true, false, false, true]
arr2 = [false, false, false ,true]

I want to make a function that is returning true only if arr1 and arr2 contains true but only one true for each array. For example for the above arrays the function must return false. If the arrays are like this:

arr1 = [true, false, false, false]
arr2 = [false, false, false ,true]

the function will return true. Do tou know a quick way for achieving this without having to count the elements?

RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • `without having to count the elements?` Regardless of the method, you'll have to iterate over each index/element until two trues are found (or the end is reached), there isn't really a way around that..? – CertainPerformance Jul 17 '18 at 07:06
  • I'm wondering if there is a quick method for it but i didnt find anything. Thanks :) – RamAlx Jul 17 '18 at 07:06
  • 1
    do you need to check two arrays at the same time? please add you attempt as well. – Nina Scholz Jul 17 '18 at 07:07
  • You might be better off [using bits and bitwise operations to act like flags](https://stackoverflow.com/a/276771/3233388) – Adelin Jul 17 '18 at 07:09
  • And I am wondering if by _“wondering if there is a quick method for it”_ you mean you are actually worried about performance for some reason … or just that you want a “quick” copy&paste ready solution that frees you from making any efforts of your own. // http://idownvotedbecau.se/noattempt/ – CBroe Jul 17 '18 at 07:09

4 Answers4

6

You can check the value of first index and last index of the element to be same:

function check(arr1, arr2){
  var firstIndexArr1 = arr1.indexOf(true);
  var lastIndexArr1 = arr1.lastIndexOf(true);
  var firstIndexArr2 = arr2.indexOf(true);
  var lastIndexArr2 = arr2.lastIndexOf(true);
  return (firstIndexArr1 !== -1 && firstIndexArr1 === lastIndexArr1) && 
  (firstIndexArr2 !== -1 && firstIndexArr2 === lastIndexArr2);
}
//return true
var arr1 = [true, false, false, false];
var arr2 = [false, false, false ,true];
console.log(check(arr1, arr2));

//return false
arr1 = [true, false, false, true];
arr2 = [false, true, false ,true];
console.log(check(arr1, arr2));

//return false if none of the array has true
arr1 = [false, false, false, false];
arr2 = [false, false, false ,false];
console.log(check(arr1, arr2));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • 1
    @NitinBisht If there is something in the middle, first index and last index will be different. – sjahan Jul 17 '18 at 07:24
  • @NitinBisht read the OP's question properly and investigate on `indexOf` and `lastIndexOf`. You will find your answer. – Ankit Agarwal Jul 17 '18 at 07:24
  • @NinaScholz iterations? There is a `&&` condition which means that if first condition fails it will not check the second comparison. You say that a lot of iterations? – Ankit Agarwal Jul 17 '18 at 08:13
1

you could search for the index of a value 2 times

function isUnique(myArray,value){
    let index = myArray.indexOf(value)
    if (index >= 0 && index+1 < myArray.length-1){
        return myArray.indexOf(value,index+1) == -1
    }
    return index >= 0 
}

// then
let result = isUnique(arr1,true) && isUnique(arr2,true)
mpm
  • 20,148
  • 7
  • 50
  • 55
  • You don't need the second part of the *if* condition. You can do everything after the *let* statement with `return index >= 0 && myArray.indexOf(value, index+1) == -1` or `return index >= 0 && myArray.lastIndexOf(value) == index`. ;-) – RobG Jul 17 '18 at 07:37
0

I think this function will do the trick.

 testFunc = () => {
   const arr1 = [true, false, false, false];
   const arr2 = [false, false, false ,true];

   return arr1.filter(val => val).length === 1 && arr2.filter(val => val).length === 1;
 };
Idan Hen
  • 437
  • 4
  • 14
  • OP's requirement is `without having to count the elements`. I think `filter` is somehow still loop thru all element and using `.length` should be a counting? – Isaac Jul 17 '18 at 07:15
  • I assumed he does not want to write the code to count it , like running a loop and count. this solves the issue. :) – Idan Hen Jul 17 '18 at 07:33
0

var arr2 = [true, false, false ,true];
var isOnlyOne = arr2.indexOf(true) != -1;
isOnlyOne = isOnlyOne ? arr2.indexOf(true)==arr2.lastIndexOf(true):isOnlyOne;
console.log(isOnlyOne );

The above logic can be used for your problem.

Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28