20

I need to test whether each item in an array is identical to each other. For example:

var list = ["l","r","b"]

Should evaluate as false, because each item is not identical. On the other hand this:

var list = ["b", "b", "b"]

Should evaluate as true because they are all identical. What would be the most efficient (in speed/resources) way of achieving this?

Penny Liu
  • 11,885
  • 5
  • 66
  • 81
Nick
  • 325
  • 2
  • 3
  • 7
  • Does this answer your question? [Check if all values of array are equal](https://stackoverflow.com/questions/14832603/check-if-all-values-of-array-are-equal) – Heretic Monkey Feb 07 '22 at 20:01

10 Answers10

17
function identical(array) {
    for(var i = 0; i < array.length - 1; i++) {
        if(array[i] !== array[i+1]) {
            return false;
        }
    }
    return true;
}
Dogbert
  • 200,802
  • 40
  • 378
  • 386
  • 1
    I opted for this solution, thanks. Pimvdb had a very elegant solution with array.every but that would of required adding a big chunk of code (for non-ES5 compliant browser support) to an already large document for just one instance of .every so I opted for this instead. – Nick Mar 10 '12 at 17:13
  • 1
    Your solution made the most sense to me. However, I'd change the "!=" to "!==" because your solution returns true in the following array [1,1,1,1,1,1,"1"] where the last character is a string. – Joffrey Baratheon Mar 21 '16 at 22:34
  • I'd like to see something similar but for objects. – MadHatter Oct 09 '17 at 05:15
  • Nice solution in traditional way. ES5 introduced Array.every(), which could be used for one linear solution as: https://stackoverflow.com/questions/9646943/check-if-each-item-in-an-array-is-identical-in-javascript/54826946#54826946 – Chang Feb 22 '19 at 12:37
16

In ES5, you could do:

arr.every(function(v, i, a) {
   // first item: nothing to compare with (and, single element arrays should return true)
   // otherwise:  compare current value to previous value
   return i === 0 || v === a[i - 1];
});

.every does short-circuit as well.

pimvdb
  • 146,912
  • 75
  • 297
  • 349
  • 2
    Nice, didn't know this existed and going to start using this myself. Just adding a pointer to the docs for array.every https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every – Shane Mar 10 '12 at 14:10
  • Thanks, that's a very elegant solution. I went for Dogberts solution in the end to avoid adding the array.every code to support older browsers - but in principle this would have worked nicely. – Nick Mar 10 '12 at 17:15
  • Here no need to compare each predecessor with the current value i.e. v===a[i-1]. Anyways a you can find one linear solution at: https://stackoverflow.com/a/54826946/8958729 – Chang Feb 22 '19 at 12:33
7

You could always do a new Set, and check the length.

var set1 = [...new Set(list)].length === 1;
Jen
  • 182
  • 1
  • 10
5

The one line answer is:

arr.every((val, ind, arr) => val === arr[0]);

You can look into Array.every for more details.

Note:

  1. Array.every is available ES5 onwards.
  2. This method returns true for any condition put on an empty array.
  3. Syntax: arr.every(callback[, thisArg]) or array.every(function(currentValue, index, arr), thisValue)
  4. It does not change the original array
  5. The execution of every() is short-circuited. As soon as every() finds an array element that doesn't match the predicate, it immediately returns false and doesn't iterate over the remaining elements
Penny Liu
  • 11,885
  • 5
  • 66
  • 81
Chang
  • 407
  • 1
  • 8
  • 16
2
arr.every(i=>i==arr[0]) //will return true if all items in arr are identical
Muhammad Raheel
  • 383
  • 2
  • 11
2
function matchList(list) {
  var listItem = list[0];

  for (index in list) {
    if(list[index] != listItem {
       return false;
    }
  }

  return true;
}
Jivings
  • 22,276
  • 6
  • 54
  • 98
1

function identical(array) {
  // a variable holding standard value
  //against this standard value we are examining the array
  var standard = array[1];
  for (var i = 0; i < array.length; i++) {
    if (array[i] !== standard) {
      return false;
    }
  }
  return true;
}

identical([1, 1, 1, 1, 1]); //return true
identical(['a', 'a', 'a']); //return true
identical(['a', 'a', 'b'])

function identical(array) {
  // a variable holding standard value
  //against this standard value we are examining the array
  var standard = array[1];
  for (var i = 0; i < array.length; i++) {
    if (array[i] !== standard) {
      return false;
    }
  }
  return true;
}

identical([1, 1, 1, 1, 1]); //return true
identical(['a', 'a', 'a']); //return true
identical(['a', 'a', 'b'])
Heretic Monkey
  • 11,078
  • 7
  • 55
  • 112
1
var list = ["b", "b", "b"];
var checkItem = list[0];
var isSame = true;
for (var i = 0; i < list.length; i++) {
  if (list[i] != checkItem) {
    isSame = false;
    break;
  }
}
return isSame;
Waynn Lue
  • 11,254
  • 8
  • 50
  • 74
0

My suggestion would be to remove duplicates (check out Easiest way to find duplicate values in a JavaScript array), and then check to see if the length == 1. That would mean that all items were the same.

Community
  • 1
  • 1
Kory Sharp
  • 480
  • 3
  • 11
0
function allEqual(list)
{
    if(list.length == 0 || list.length == 1)
    {
      return true;
    }

    for (index in list) {
    if(list[index] != list[index+1] {
       return false;
    }
  }

  return true;

}
Erix
  • 6,857
  • 2
  • 33
  • 60