-1

Given a (JS) array of integers, I am trying to ascertain if it is zer0-balanced -- that is, for any element x there is a corresponding element of -x - in a single line of code.

I would think you could do this using every() and includes() but can't quite get the right logic.

arr.every(n => arr.includes(-n)) obviously does not work because includes() is not a function, but that's the idea I've been failing least with. where to take this next? thank you!

  • 2
    You can write anything in JavaScript in one line by removing all line breaks. That's a useless requirement. – Ingo Bürk Oct 26 '17 at 15:20
  • [`Array.includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) is a function, but must be called in an environment that supports ES2016. This can be swapped out for `.indexOf()`. `arr.every(n => ~arr.indexOf(-n))` – mhodges Oct 26 '17 at 15:20
  • Your requirement isn't clear. The array `[1, 1, -1]` has the property that for every element x there is a corresponding element -x, but it isn't zero-balanced. Which do you actually need? Why does it have to be a single line of code? – Ted Hopp Oct 26 '17 at 15:34

3 Answers3

0

You could use a hash table and count with increment with positive numbers and decrement with negative numbers. If done, check all properties and return the nefgated check with Array#some.

function isBalanced(array) {
    var hash = {};
    array.forEach(a => hash[Math.abs(a)] = (hash[Math.abs(a)] || 0) + Math.sign(a));

    return !Object.keys(hash).some(k => hash[k]);
}

console.log(isBalanced([1, 2, -2, -1]));
console.log(isBalanced([1, 2, -2, -1, -1, 1]));
console.log(isBalanced([1, 2, -2]));
console.log(isBalanced([1, 2, -2, 1, 1]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Ignoring the fact that this is question has been asked numerous times over the years, the statement "obviously does not work because includes() is not a function" leads me to believe that the OP is not running this in an environment that supports ES2016 features. – mhodges Oct 26 '17 at 15:24
  • that's very cool. could you explain const checkNegative = (n, _, a) => a.includes(-n) works and const checkNegative = (n, a) => a.includes(-n) doesn't? – DCR Oct 26 '17 at 15:25
  • it is the callback structur which is at the first place the element, then comes the actual index and then tha calling array. theat is the reason why it does not work with index, but with array. – Nina Scholz Oct 26 '17 at 15:28
  • @mhodges, arrow function indicates ES6. – Nina Scholz Oct 26 '17 at 15:28
  • @DCR 3 parameters get passed to the `.every()` callback. the element, the index, and the original array. Nina labeled these as n, _, and a, but they can be named anything - it's order that matters. – mhodges Oct 26 '17 at 15:29
  • 1
    @NinaScholz ES6 !== ES2016 – mhodges Oct 26 '17 at 15:29
  • @mhodges, you are right. – Nina Scholz Oct 26 '17 at 16:50
0

If your environment has Array.every but not Array.includes, that's strange, but you can use Array.some instead of includes like so

arr.every(n => arr.some(x => x === -n))
TKoL
  • 13,158
  • 3
  • 39
  • 73
  • This will accept `[1, 1, -1]` and it isn't clear if OP wants to accept that array. – Ted Hopp Oct 26 '17 at 15:35
  • That was my thought before I posted this too, but given that OP was intending on using the function 'includes', which would produce that behavior, I thought it was appropriate. – TKoL Oct 26 '17 at 15:47
  • the error thrown for this line of code in my console is the same as for `every()` -- "Uncaught TypeError: n.some is not a function" – michellegee Oct 26 '17 at 16:02
  • If you google each of these javascript functions, you can find polyfills that you can copy and paste into your .js files – TKoL Oct 26 '17 at 16:13
0

You can use every with indexOf and a reduce statement to check if entire array actually sums up to zero as first pre-condition.

Using console.log, you can see it happen for every item in array in console.

If it does not find a matching negative element at any point, it stops execution there.

const arr = [1, 4, 8, -4, -8, -1,13,-13];
console.log((arr.reduce((x, y) => x + y) == 0) && arr.every(n => {console.log(n); return arr.indexOf(-n) > -1; }));
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22