Algorithm : Find Number Of Duplicates in Arrays



function sameElementsNaive(a, b) {    
    var count =0;
    for(i=0;i<a.length;i++){
        for(j=0;j<b.length;j++){
            if(a[i]==b[j]){
                count++;
                break;
            }
        }
    }
    return count;
}

Here Two inputs to the function are integer arrays (can be change to other data types as well).

Ex:
sameElementsNaive([1,2,3], [3,4,5]);
this function call should return 1 because only 3 is duplicated in the second array.

No comments:

Post a Comment