0
var valueArray = ['ABC','DEF','GHI','ABC','JKL','MNO','DEF'];
var flag =false;
for(var i=0; i<valueArray.length; i++)
{
   for(var j=0; j<valueArray.length; j++)
  {
     if(valueArray[j] == valueArray[i] && j != i)
     {
          flag  = true;
          break;
     }
  }
}
if(flag)
    alert('same values found');

I am trying to validate one array by checking for duplicate values, i used above code, i don't think its a better way. is there any ways with jquery for this or some good js codes for it.

5
  • This question appears to be off-topic because it is about code review Commented Jun 25, 2014 at 6:01
  • 1
    @chriz NOPE.. jsfiddle.net/C7rK9 Commented Jun 25, 2014 at 6:05
  • @RajaprabhuAravindasamy but i dont want to remove duplicates just want to check.. Commented Jun 25, 2014 at 6:09
  • "Better" by what criteria? Faster? Easier to understand? Easy to maintain? Most compatible with old browsers? Not dependent on a library? Commented Jun 25, 2014 at 6:14
  • @RobG FASTER and easy to understand Commented Jun 25, 2014 at 6:17

3 Answers 3

1

Not sure about jquery, but performance will be better with just one for loop:

for(var i = 0; i < valueArray.length; i++)
{
  if (valueArray.indexOf(valueArray[i], i+1) != -1) {
    flag = true;
    break;
  }
}

jsPerf test: http://jsperf.com/check-for-double-occurences

Sign up to request clarification or add additional context in comments.

3 Comments

@openorclosw great impressed
The tests are not particularly good since the first element in the array is the duplicated element, so methods like indexOf and using sort are only called once, whereas the looping methods are called at least 4 times. Much fairer to put the duplicate in the middle to emulate an average case where the duplicate could appear anywhere in the array. Compare with the results here. Also, performance is hugely affected by the browser in use.
Yeah no wonder my answer was so much faster than yours so it was just due to the browser
1

If you want a fast, compatible, "works everywhere" function that just checks for duplicates and doesn't require any library, consider:

function hasDups(arr) {

  // Firstly copy array so don't affect original, then sort
  var t = arr.slice().sort();

  // If adjacent members have the same value, return true
  for (var i=1, iLen=t.length; i<iLen; i++) {
    if (t[i] === t[i-1]) return true;
  }
  return false;
}

console.log(hasDups(['abc','dvf','abc'])); // true

However you might want something a little more functional, e.g. that you can provide a compare function to so that, say, 'abc' == 'ABC' or '5' == 5.

Or if you want to use new features and avoid the copy and sort, consider:

function hasDups2(arr) {
  var obj = {};
  return arr.some(function(v){
    if (obj.hasOwnProperty(v)) return true;
    obj[v] = '';
  });
}

The same strategy can be applied to the first as well and avoid ES5's some.

Note that both the above are only really suitable for comparing primitive values, though the first is better for that. If you want a reliable function to look for duplicate objects, that requires a bit more work.

2 Comments

sry i think openorclose's answer deserves accept.. its more simple.
@chriz—how so? It's more code and runs at 1/3 to 1/4 the speed of the above solutions (depending on the browser), see http://jsperf.com/has-duplicates.
0

Use jquery.unique() and compare the array size. If the size of the resultant array is not the same then return false.

Doc for jquery.unique()

1 Comment

@Chris: Actually tested this. It is working. However you are right as this may not be an efficient answer since it is not meant for the purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.