-2

I have any array

var myArr = [1,1,1,1,1];

if all the elements in an array are same, then we return true and else false.

eg : myArr[1,1,1,1,1] return true;

myArr[1,2,1,1,1] return false;

for(var i=0; i<myArr.length; i++){
    if(myArr[i] != myArr[i+1]){
        flg = false; 
    }

}

Can anyone help me to design this code.

amateur_me
  • 55
  • 6
David
  • 4,266
  • 8
  • 34
  • 69
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Søren D. Ptæus Jun 27 '18 at 10:32

7 Answers7

0

That's why .every is there

var myArr = [1,1,1,1,2 ];

var myArr2 =  [1,1,1,1,1,2,3,4,5];

console.log(myArr.every(o=> myArr2.indexOf(o) >=0 ))
Muhammad Usman
  • 10,039
  • 22
  • 39
  • I would update your answer to display what the user is asking. He's not mentioning a second array. While your answer shows how to use it for checking more variables, this could be fine. But you should answer the main question first...which is just for an element. Secondly, if you are going to show how to check an array vs an array, please show the user how using `.every` will be able to say which index/value failed the test rather than just saying `false`. – Matt Jun 27 '18 at 11:36
  • @Matt this is what I got by _all the element in array then we return true and else false_ – Muhammad Usman Jun 27 '18 at 11:39
  • OP is only interested in if all elements in the given array are all the same. For that they are iterating a single array checking if `i+1` is equal to current. – Matt Jun 27 '18 at 11:41
  • In that case its a duplicate of https://stackoverflow.com/questions/14832603/check-if-all-values-of-array-are-equal – Muhammad Usman Jun 27 '18 at 11:43
0

Create a function and return false if any element of a array is not equal to 1 .

function retStat( myArr ){

 for(var i=0; i<myArr.length; i++){
     if(myArr[i] != 1){
         return false;
     }

 }

 return true;

}

arr1 = [1,1,1,1,1];
arr2 = [1,2,1,1,1];

console.log( retStat( arr1 ) ); // true
console.log( retStat( arr2 ) ); // false
Tamim
  • 916
  • 11
  • 19
  • I wouldn't say `myArr[i] != 1` as his next check may not be looking for a `1`. He very well may, but it's not stated and based on his example having a `2` in it then it's open for other values. I would make `1` more dynamic like next element..or start the loop at index 1 and always checking against index 0. – Matt Jun 27 '18 at 11:51
0

Small mistake, you are checking for next element myArr[i+1] which is undefined at last loop

Check with condition myArr.length-1

for(var i=0; i<myArr.length-1; i++){
    if(myArr[i] != myArr[i+1]){
        flg = false; 
        break;
    }
}
Jagdish Idhate
  • 7,513
  • 9
  • 35
  • 51
  • 1
    Would also like to mention that you can just short circuit the loop after you find that the flag has been set. flg will not become more false than just false. – Matt Jun 27 '18 at 11:49
0

This can also be achieved without loop. You can check for the first element to the entire string represenattion of the array. If the match is found for all characters in the string it will give you blank value as ''. Using this you can know if the array has all same elements or there is different elements present there.

function isSame(arr){
  var str = arr.join('');
  var re = new RegExp(arr[0],"g");
  var replacedStr = str.replace(re,'');
  return !Boolean(replacedStr);
}

var myArr = [1,1,1,1,1];
console.log(isSame(myArr));
myArr = [1,2,1,1,1];
console.log(isSame(myArr));
myArr = [1,1,1,5,1];
console.log(isSame(myArr));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • I know nothing of regex and how replace works behinds the scene so just asking, but does replace loop the string in order to find the character that matches? – Matt Jun 27 '18 at 11:48
0

Since you want to check whether all elements in array are the same you can compare all of them to first element. Function some returns as its callback returns true, so callback isn't called after first mismatch is found.

console.log(check([1,1,1]));
console.log(check([1,2,3]));
console.log(check([]));

function check(arr){
  if (!arr.length){
    return false;
  }
 return !arr.some(el=> el !== arr[0]);
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
0

I think this works:

<script type="text/javascript">
var myArr = [1,1,1,1,1];
flg=true;
for(var i=0; i<myArr.length; i++)
{
    if(i==(myArr.length-1))
    {
        if(myArr[i]!=myArr[i-1])
        {
            flg=false;
        }
    }
    else
    {
        if(myArr[i] != myArr[i+1])
        {
            flg=false;
            return false;
        }
    }
}
console.log(flg);
</script>
  • If doing a normal for-loop and you are already flagging false, just short circuit the loop. If there are a million elements and you find the answer after 3 iterations...no point in going through the other ~million elements to conclude the same result. Now if you were marking which ones weren't the same then that would be different...but better to short circuit. – Matt Jun 27 '18 at 11:44
  • With millions of elements, a different algorithm, viz., Binary search could be used. – harshita_ahuja Jun 27 '18 at 12:03
0

Here is a solution inspired by this answer https://stackoverflow.com/a/9229821/5061000

What does the below function in snippet do?

  • Removes all the duplicate items,
  • Returns true if .length == 1 (i.e. all values are the same!).

function array_all_same(a) {
  return a.filter(function(item, pos) {
    return a.indexOf(item) == pos;
  }).length == 1;
}


var myArr1 = [1, 1, 1, 1, 1];
console.log(array_all_same(myArr1));

var myArr2 = [1, 2, 1, 1, 1];
console.log(array_all_same(myArr2));

Hope it helps!

Takit Isy
  • 9,688
  • 3
  • 23
  • 47