0

In my code I need to find the repeated value and give an alert using jQuery. Below is the example arrays and my requirement. Please help.

            a1 = {1,2,3,4,5,6,4}
            a2= {9,8,7},
            a3= {a,b,c,d,e,7}

In the above arrays I need to get the value 4 and give alert because it is repeating in array "a1" and I need to get the value 7 because it is repeating in the arrays 'a2' and 'a3'.
The first issue I fixed like as follows. Ineed to to fix the second one.

        for (var $i = 0; $i<= $last; $i++)
        {
        var hours = [];
        var minutes = [];
        var activeTime = [];    
        $.each($('.hour'+$i),function() {
       hours.push($(this).val());    
         });
       $.each($('.hour'+$i).next('select'),function(){
        minutes.push($(this).val());
       });
       for ( var i = 0; i < hours.length; i++ ) {
          activeTime.push(hours[ i ]+":"+minutes[ i ]+":"+"00");
         }
         for ( var i = 0; i <= hours.length; i++ ) {
          if ( hours[ i ] === "" )
           {
          $("#timeValidate"+$i).css("display", "block");
           return false;
           }
            else
             {
            $("#timeValidate"+$i).css("display", "none");
            }
            }
          for(var i=0; i< activeTime.length; i++)
          {
           for(var j = 0; j< activeTime.length; j++)
           {
          if( i != j)
             {
           if(activeTime[j] == activeTime[i])
              {
            $("#timeValidate"+$i).text("");
            $("#timeValidate"+$i).text("active time"+activeTime[j]+" is repeating");
             $("#timeValidate"+$i).css("display", "block");
            return false;
            }
            }
             }
            }
           }
6
  • 3
    Those don't look like arrays, in fact that's invalid syntax. Commented Nov 27, 2012 at 9:18
  • 2
    What have you tried? Commented Nov 27, 2012 at 9:18
  • it is just an example not exact code Commented Nov 27, 2012 at 9:19
  • the correct format is a= [1,2,3,4,5,6,4], '[]' not '{}' Commented Nov 27, 2012 at 9:19
  • Hint: concat your arrays before checking for duplicates. That way you don't have to care about whether or not the duplicates are in different source arrays. Commented Nov 27, 2012 at 9:21

3 Answers 3

1
function getDuplicatesFromArrays() {
    var allItems = Array.prototype.concat.apply([], arguments);
    var duplicates = [];
    var hash = {};

    allItems.forEach(function(x) {
        if(hash.hasOwnProperty(x)) duplicates.push(x);
        hash[x] = 1;
    });

    return duplicates;
}

The above function accepts any number of arrays, and yields the duplicates:

getDuplicatesFromArrays(a1,a2,a3) // [4, 7]

Demo

It works as sort of an inverse hash sieve; adding to duplicates only if the item was already in the hash.

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

2 Comments

How to call the function with 'n' number of array using a loop
@Mansoor: the function accepts any number of arrays getDuplicatesFromArrays(a,b,c,d,e,f,g,h,i,j,k) would be fine, if those are all arrays.
0

Try sugar.js, it has advanced functions for arrays (and others). For example, you can use a combination of unique() and subtract() to get an array that contains only the elements that are repeated. Then you can parse it and alert for each one.

Comments

0

Here you go:

var original = [].concat(a,b,c),
    dupes = [];
for (var c = 0; c < original.length; c++) {
    if (original.filter(function(v) {
        return v == original[c];
    }).length > 1) {
        dupes.push(original[c]);
    }
}
alert($.grep(dupes, function(v, k){return $.inArray(v , dupes) === k;}));

Here is a demo: http://jsfiddle.net/q2c42/

6 Comments

There will be n times of array then how we will try this
@Mansoor Updated and provided an example. Also alerts the dupes only once.
This demo yields an empty array for me.
@DavidHedlund Gives me [4,7], although the previous version had this problem. Try refreshing the page and trying the demo (this one)
@Asad: yes, your demo isn't updated so I used the code in your answer. It works, if I replace arr with original and dupes, respectively.
|

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.