21

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
  • 15,447
  • 5
  • 79
  • 98
Nick
  • 335
  • 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

11 Answers11

19

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
  • 151,816
  • 78
  • 307
  • 352
  • 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
17
function identical(array) {
    for(var i = 0; i < array.length - 1; i++) {
        if(array[i] !== array[i+1]) {
            return false;
        }
    }
    return true;
}
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 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
12

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

var set1 = [...new Set(list)].length === 1;
Jen
  • 352
  • 3
  • 12
8

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
  • 15,447
  • 5
  • 79
  • 98
Chang
  • 435
  • 1
  • 8
  • 17
4
arr.every(i=>i==arr[0]) //will return true if all items in arr are identical
Muhammad Raheel
  • 617
  • 1
  • 6
  • 15
2
function matchList(list) {
  var listItem = list[0];

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

  return true;
}
Jivings
  • 22,834
  • 6
  • 60
  • 101
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,687
  • 7
  • 53
  • 122
Izhar Ul Haque
  • 116
  • 1
  • 5
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,344
  • 8
  • 51
  • 76
0

To check if all values in an array are equal in JavaScript, you can use the every() method in combination with the === operator to compare each element of the array to the first element, like this:

function allEqual(arr) {
  return arr.every(val => val === arr[0]);
}

This function takes an array arr as its parameter and returns true if all values in the array are equal, and false otherwise.

Here's an example usage:

const arr1 = [1, 1, 1, 1];
const arr2 = [1, 2, 3, 4];
const arr3 = ['foo', 'foo', 'foo'];

console.log(allEqual(arr1)); // true
console.log(allEqual(arr2)); // false
console.log(allEqual(arr3)); // true

In the example above, arr1 and arr3 contain only equal values, so the allEqual() function returns true for both of them. arr2 contains different values, so the function returns false.

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
  • 490
  • 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
  • 7,059
  • 2
  • 35
  • 61