8

How do I check if my array contains only one distinct element? `

var arr = [1, 1, 1];
var arr2 = [1, 2, 3];
checkIfThereIsOnlyOneElement(arr); //should return true
checkIfThereIsOnlyOneElement(arr2) //should return false;`
David
  • 208,112
  • 36
  • 198
  • 279
stephenpassero
  • 431
  • 1
  • 5
  • 16

8 Answers8

6

Use Set object:

var arr = [1, 1, 1],
    arr2 = [1, 2, 3],
    checkForOne = function(arr){
        return (new Set(arr)).size === 1;
    }
    
console.log(checkForOne(arr));
console.log(checkForOne(arr2));
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
3

You could implement it this way:

var arr = [1, 1, 1];
var arr2 = [1, 2, 3];

console.log(checkIfThereIsOnlyOneElement(arr));
console.log(checkIfThereIsOnlyOneElement(arr2));

function checkIfThereIsOnlyOneElement(arr) {
    arr.sort();
    return arr[0] == arr[arr.length -1]
}
TobiasR.
  • 826
  • 9
  • 19
  • Very nice. Sort the array and then check to see if the first and last item are the same. The other answers complicate things. – castis Apr 12 '17 at 12:55
  • 1
    @castis What if the array is one thousand elements long? I just guess the sorting function will be an overkill. The `Set` method seems to be much better, imo. – kind user Apr 12 '17 at 12:56
  • Well the OP had an example 3 elements long. No sense in over-engineering something just for that. If OP has a huge performance issue I assume they'll be back. – castis Apr 12 '17 at 12:59
0

try this simple implementation

function checkIfThereIsOnlyOneElement(arr)
{
  var map = {};//create a map
  for( var counter = 0; counter < arr.length; counter++)
  {
    map[ arr[ counter ] ] = true;
    if ( Object.keys ( map ).length > 1 ) //check if this map has only one key
    {
      return false;
    }
  }
  return true;
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

simply check if all elements contain the same value, one way is to use "uniq" function of "lodash" module:

checkIfThereIsOnlyOneElement(arr){
     return _.uniq(arr).length == 1; 
}
Mohiransh
  • 1
  • 2
0

You can check unique values in array. Based on this term we can implement your function:

var arr = [1, 1, 1];
var arr2 = [1, 2, 3];

function checkIfThereIsOnlyOneElement(arr) {
    return arr.filter(function (item, i, ar) {
            return ar.indexOf(item) === i;
        }).length === 1;
}

console.log(checkIfThereIsOnlyOneElement(arr));
console.log(checkIfThereIsOnlyOneElement(arr2));

jsfiddle

Alex Slipknot
  • 2,439
  • 1
  • 18
  • 26
0

There are plenty of pretty easy implementations for this function. Let me give you two, one is a functional one-liner, the other is an iterative (probably better performing) snippet.

By the way, your naming is very bad, it should be called: isArrayOfTheSameElements

  // Iterative:
  function isArrayOfTheSameElements(arr){
    var firstElement = arr[0];
    for(var i=1; i < arr.length; i++){
      if (arr[i]!==firstElement) {
        return false;
      }
    }
    return true;
  }

  // Functional:
  function isArrayOfTheSameElements(arr){
    return Object.keys(arr.reduce(function(current, next){
      return current[next] = next && current;
    }, {})).length <= 1;
  }
Alex
  • 259
  • 3
  • 7
0

You could use Array#every and check only agsindt the firsr element.

function checkDistinct(array) {
    return array.every(function (a, _, aa) { return a === aa[0]; });
}

var arr = [1, 1, 1],
    arr2 = [1, 2, 3];

console.log(checkDistinct(arr));  // true
console.log(checkDistinct(arr2)); // false
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can always properly do like .every() as pointed out by @Nina Scholz but for a variety you may also do like;

var all = a => a.length-1 ? a[0] === a[1] && all(a.slice(1)) : true;
console.log(all([1,1,1,1]));
console.log(all([1,1,1,2]));

It has the same benefit to cut the iteration once it meets a false value.

Community
  • 1
  • 1
Redu
  • 25,060
  • 6
  • 56
  • 76