admin管理员组文章数量:1432640
What is a quick way to determine if an array is only posed of 0's and 1's?
I'm vaguely familiar with a boolean solution but am not away how to implement it. In order to implement is you would have to assign values to the boolean
true = 0
and
false = 1
But how would you implement this if you are given an array such that
array = [1,1,1,1,0,0,0,0]
isArrayBool (){
}
What is a quick way to determine if an array is only posed of 0's and 1's?
I'm vaguely familiar with a boolean solution but am not away how to implement it. In order to implement is you would have to assign values to the boolean
true = 0
and
false = 1
But how would you implement this if you are given an array such that
array = [1,1,1,1,0,0,0,0]
isArrayBool (){
}
Share
Improve this question
asked Apr 9, 2018 at 23:58
William MerrittWilliam Merritt
4391 gold badge5 silver badges13 bronze badges
7
-
3
array.every(v => v === 0 || v === 1)
– Jaromanda X Commented Apr 9, 2018 at 23:58 -
note ...
true=1
andfalse=0
is more logical ... especially since+true
===1
and+false
===0
– Jaromanda X Commented Apr 10, 2018 at 0:02 -
Faster way would be to use a
for
loop. – ibrahim mahrir Commented Apr 10, 2018 at 0:02 -
yes, because using .every on an 8 length array is so much slower than a for loop ... nanoseconds are precious ...
array.some(v => v !== 0 && v !== 1);
better? – Jaromanda X Commented Apr 10, 2018 at 0:03 - 2 @ibrahimmahrir - I see why you say a for loop is faster - because Chrome native array methods (some, every, etc) are pitifully slow – Jaromanda X Commented Apr 10, 2018 at 0:28
3 Answers
Reset to default 3Very very naive solution:
function isArrayBool(array) {
for (var i of array) {
if (i !== 0 && i !== 1) return false;
}
return true;
}
console.log(isArrayBool([1,0,0,0,1])); // true
console.log(isArrayBool([1])); // true
console.log(isArrayBool([2,3,0])); // false
So I threw a few functions together in jsperf to test. The fastest one I found so far is the one below (which is much faster than the for of
version):
function isBoolFor(arr) {
for(var i=arr.length-1; i>=0; --i){
if(arr[i] !== 0 && arr[i] !== 1) return false
}
return true
}
Comparison is here
EDIT: I played around with another version that turned out to be quicker:
function isBoolFor(arr) {
for(var i=0; arr[i] === 0 || arr[i] === 1; i++){}
return i === arr.length
}
The key here is that because most of the array you are checking will consist of zeros and ones, you can avoid doing two boolean checks every iteration by using the ||
instead of the &&
negative check. It is only a subtle improvement, and could be argued to be no better than the one above in practicality.
UPDATE: So the difference between all the for and while variants is too subtle to declare an overall winner. So in that case I would go for readability. If you want binary use typed arrays!
FINAL UPDATE:
I was curious and wanted to find an even faster version, and it can be done if the array is known to be only numbers. If the array contains only numbers, then you can use a bitwise operator check for either of the values in question. For example:
function isBoolFor(arr) {
const test = ~0x01
for(var i=arr.length-1; i>=0; --i){
if(test & arr[i]){ return false }
}
return true
}
https://jsperf./bool-array-test-2/1
As we can see here, the fastest way to do this can be seen here... With a simple for loop, as suggested by user Jaromanda in a ment. This for loop blows other solutions out of the water in terms of speed.
var someArray = [1,0,1,1,1,1,0,0,1,0,1,0,0,1,0,1,0,1,1,1,1,0,1,1,0,1,0];
function simpleForLoop(array){
var numArgs = someArray.length;
for(var loop = 0; loop < numArgs; loop++){
var arg = someArray[loop];
if(arg !== 0 && arg !== 1){ return false; }
}
return true;
}
var isbool = simpleForLoop(someArray);
本文标签: Fast way to check if a javascript array is binary (contains only 0 and 1)Stack Overflow
版权声明:本文标题:Fast way to check if a javascript array is binary (contains only 0 and 1) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745596552a2665515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论