7

I have a jQuery array:

var arr = $('input[name$="recordset"]');

I am getting the value of array like 8 or 6

If array values are repeating or duplicate I need to show "please do not repeat the values". If not I need to proceed further.

Using jQuery can anybody tell me how to find the duplicate values?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
kumar
  • 2,944
  • 18
  • 60
  • 89
  • http://api.jquery.com/jQuery.unique/ might be helpful – jAndy Dec 03 '10 at 14:36
  • 1
    @kumar That's not an array. It's a jQuery object. – Šime Vidas Dec 03 '10 at 14:36
  • Thanks JAndy.. Jquery.Unique is work for DOM elements. – kumar Dec 03 '10 at 14:37
  • @kumar jQuery.unique will remove duplicate DOM elements. But there's no way that you have duplicate DOM elements when you use a selector. – Šime Vidas Dec 03 '10 at 14:38
  • 1
    I **think** what is OP is saying is that he's calling `arr.val()`, and it returns a number in the input; he wants to check for duplicate **input values**. – Matt Ball Dec 03 '10 at 14:39
  • possible duplicate of [Easiest way to find duplicate values in a javascript array](http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array) – epascarello Dec 03 '10 at 15:10

4 Answers4

14
var unique_values = {};
var list_of_values = [];
$('input[name$="recordset"]').
    each(function(item) { 
        if ( ! unique_values[item.value] ) {
            unique_values[item.value] = true;
            list_of_values.push(item.value);
        } else {
            // We have duplicate values!
        }
    });

What we're doing is creating a hash to list values we've already seen, and a list to store all of the unique values. For every input the selector returns we're checking to see if we've already seen the value, and if not we're adding it to our list and adding it to our hash of already-seen-values.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • 1
    @Sean I recommend this: `var val = $(item).val();` and then work with val instead of item.value. The reasoning behind this: the value of a form element is not always in its value attribute - it can be the text-content of the element, also. – Šime Vidas Dec 03 '10 at 14:54
  • @Šime - I cannot say I've ever encountered that. For my information, what form elements would this apply to? – Sean Vieira Dec 03 '10 at 14:56
  • @Sean I'll go check... give me a sec. – Šime Vidas Dec 03 '10 at 14:57
  • 1
    @Sean Using item.val is safe in Chrome. But I remember having an issue in an older version of IE. Unfortunately I only have IE9 beta here, so I cannot confirm it. – Šime Vidas Dec 03 '10 at 15:26
  • @Šime -- fair enough. I'll dig into it some more. Thanks for checking! – Sean Vieira Dec 03 '10 at 15:43
  • If you don't need to use the list_of_values array for anything later on but simply want to ensure you remove dupes, you could eliminate any lines above containing list_of_values. This is useful for instance if you're iterating LIs or DIVs and want to eliminate a dupe element. – Volomike May 02 '12 at 04:07
2

Hope that below snippets will help if someone looks for this kind of requirement

var recordSetValues = $('input[name$="recordset"]').map(function ()    {
          return this.value;
      }).get();     
var recordSetUniqueValues = recordSetValues.filter(function (itm, i,    a) {
          return i == a.indexOf(itm);
      });
if (recordSetValues .length > recordSetUniqueValues.length)
      { alert("duplicate resource") }
Senthil
  • 1,499
  • 16
  • 17
1
$('form').submit(function(e) {

    var values = $('input[name="recordset[]"]').map(function() {
      return this.value;
    }).toArray();

    var hasDups = !values.every(function(v,i) {
      return values.indexOf(v) == i;
    });
    if(hasDups){
       // having duplicate values
       alert("please do not repeat the values");
       e.preventDefault();
    }

});
Needhi Agrawal
  • 1,326
  • 8
  • 14
1
// For every input, try to find other inputs with the same value
$('input[name$="recordset"]').each(function() {
   if ($('input[name$="recordset"][value="' + $(this).val() + '"]').size() > 1)
      alert('Duplicate: ' + $(this).val());
});
Thiago Belem
  • 7,732
  • 5
  • 43
  • 64