Javascript: Find out if we have at least two different values in an array - Stack Overflow

My aim is to find out if we have at least two different values in an array. How to find out this using

My aim is to find out if we have at least two different values in an array. How to find out this using pure javascript. By now, I use this funcion, who tells me if there are repeated values in an array...

function verificar(array)
{

var filtrado_arr = array.sort(); 
var resultados = [];
for (var i = 0; i < array.length - 1; i++) {
     if (filtrado_arr[i + 1] == filtrado_arr[i]) {
         resultados.push(filtrado_arr[i]);
 }
 }
  if (resultados.length > 0)
 {
  alert("Repeated results"+resultados+".Unable to send to graph.");
  return false;
  }
  else
  {
  alert("No repeated measures, we can set graph");
  return true;
  }
 }

But this is not enought, of course.

My aim is to find out if we have at least two different values in an array. How to find out this using pure javascript. By now, I use this funcion, who tells me if there are repeated values in an array...

function verificar(array)
{

var filtrado_arr = array.sort(); 
var resultados = [];
for (var i = 0; i < array.length - 1; i++) {
     if (filtrado_arr[i + 1] == filtrado_arr[i]) {
         resultados.push(filtrado_arr[i]);
 }
 }
  if (resultados.length > 0)
 {
  alert("Repeated results"+resultados+".Unable to send to graph.");
  return false;
  }
  else
  {
  alert("No repeated measures, we can set graph");
  return true;
  }
 }

But this is not enought, of course.

Share Improve this question asked Aug 12, 2013 at 5:30 ViceNocillatorViceNocillator 1051 gold badge3 silver badges11 bronze badges 3
  • 3 if size of array greater than 1, after sorting pare last element with first element – 999k Commented Aug 12, 2013 at 5:34
  • Why bother sorting, unless it's needed for something not shown? Just pare the first element of the array to each other element until either (a) a difference is found or (b) all have been pared and no diff was found. – atk Commented Aug 12, 2013 at 5:39
  • 1 Also, the second print is misleading based on your stated goal. Second print sounds like there are zero duplicates, while stated goal is that there are at least two that aren't duplicates. A, AM B meets stated goal but not second print. A, B C meets second print. – atk Commented Aug 12, 2013 at 5:42
Add a ment  | 

4 Answers 4

Reset to default 4

Using a sort and an extra array seems like an overly expensive way to perform this simple task. What is wrong with the following?

function verificar(arr)
{
   for (var i=1; i<arr.length; i++) {
      if (arr[i-1] != arr[i])
         return true;
   }
   return false;
}
function verificar(array) {

   for(i=1; i<array.length; i++){
      if(array[0] !== array[i])
         return true;
   }

   return false;
}

Your code implies that you want to check for duplicates, not verify that you have at leas one unique pair of values as stated in your question.

I'd just add another method to the pool of answers: checking for duplicates using Array.some():

function hasDuplicates(array){
    return array.some( function( elm, idx ){
        return array.lastIndexOf( elm ) > idx;
    });
}
console.log( hasDuplicates([3,4,1,2]) ); // false
console.log( hasDuplicates([3,4,1,3,2]) ); // true

Just check the first value of the array "filitrado_arr[0]" against all of the other values in the array. If one of the values matches the first value of array, you know that there is a repeated value. Here is an example of how you could implement that logic:

    function verificar(array){ 
    var repeats = false
    for (var i = 1; i < array.length; i++) {
         if (array[0] == array[i]) {
             repeats = true;
             return repeats
     }
     }
}

However this answer matches the goal implied by the alerts in your original function not the goal in the question itself.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355349a4624082.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信