-4

I am validating an array element is having duplicate value. I have multiple objects as {S:1,R:2,V:3} in a array. I want to throw an alert message for if "S" element having duplicate value in that array.

What I did:

var arr=[{S:1,R:2,V:3},{S:2,R:2,V:3},{S:1,R:4,V:5},{S:3,R:2,V:3}, 
         {S:2,R:2,V:3},{S:3,R:4,V:5}];
function duplicateValidation()
{
var sorted_arr = arr.slice().sort();
var results = [];
for (var i = 0; i < sorted_arr.length - 1; i++) {
    if (sorted_arr[i + 1].S == sorted_arr[i].S) {
        results.push(sorted_arr[i]);
         break;
    }
}

   console.log(results);
   return results;
}

if(duplicateValidation().length==1)
{
alert("S -" + duplicateValidation()[0].S +" is duplicate");
}

But the above snippet (from this answer) does not working for me. I am expecting S - 1 is duplicate in a alert message.

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • let me know the reason for negative vote.. – Ramesh Rajendran Oct 04 '18 at 10:21
  • There are probably a few different things that might be triggering the downvotes. For example, this looks like (unstated) homework and some of the tags don't make sense. Anyway...the code from the other SO answer depends on the array being sorted; is the array being correctly sorted in your scenario? – Ian Gilroy Oct 04 '18 at 10:42

2 Answers2

1

change your for with this your task can achieve with nested for instead of one for loop

var arr=[{S:1,R:2,V:3},{S:2,R:2,V:3},{S:1,R:4,V:5},{S:3,R:2,V:3}, 
         {S:2,R:2,V:3},{S:3,R:4,V:5}];
function duplicateValidation()
{
    var sorted_arr = arr.slice().sort();
    var results = [];
    for (var i = 0; i < sorted_arr.length - 1; i++) {
    var S_type = sorted_arr[i].S;
    for (var j = i; j < sorted_arr.length - 1; j++){
        if (sorted_arr[j + 1].S == S_type) {
            results.push(sorted_arr[i]);
            break;
        }
    }
 }       
   return results;
}

if(duplicateValidation().length > 1)
{
console.log("S - " + duplicateValidation()[0].S +" is duplicate");
}
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
Abdullah Khan
  • 649
  • 5
  • 11
  • But this indicates that there is only 1 duplicate - there are infact 3 duplicated S values - as shown in my solution. – gavgrif Oct 04 '18 at 10:46
  • @gavgrif Yes. But you are showing result of ` index (2, 4, 5)` index's. But the first index element s a duplicate element from other elements. I am expecting index `(1,2,4)` – Ramesh Rajendran Oct 04 '18 at 10:49
  • This gives different output on different browsers (try IE :D). The reason is that unnecessary `sort` call that may do nothing or may sort this array in some weird way – barbsan Oct 04 '18 at 10:52
  • @ramesh-rajendran - the problem is that the item at index 1 IS NOT A DUPLICATE - its the first instance of the S:2 value... the second instance of that value is at index 4 and that is the duplicate of that value. – gavgrif Oct 04 '18 at 10:53
  • @RameshRajendran in this solution 1 is value of duplicate S, not index of that object. (and you seem to not understand that index is 0-based) – barbsan Oct 04 '18 at 11:08
0

You don't need to sort the array - all that does is another round of iterating over the content - simply do it once and you ccan check for duplicates without sorting.

By iterating over the array and finding each items value of "S", and checking whether it has already been pushed into a results array - and if it has - push the duplicated item's index into a duplicates array. Then if there are duplicates - you know the index of each duplicate.

var arr=[{S:1,R:2,V:3},{S:2,R:2,V:3},{S:1,R:4,V:5},{S:3,R:2,V:3}, 
         {S:2,R:2,V:3},{S:3,R:4,V:5}];

var results = [];
var duplicates = [];
arr.forEach(function(obj, index){
  results.indexOf(obj.S) == -1
  ? results.push(obj.S)
  : duplicates.push(index)
})

var duplicatesLength = duplicates.length;
duplicates.length > 0
 ? console.log(duplicatesLength + ' duplicates found at index (' + duplicates .join(', ')+')')
 : console.log('No duplicates found')

//gives 3 duplicates found at index (2, 4, 5)
// ie: item 0 = item 2, item 1 = item 4, item 3 =  item 5
gavgrif
  • 15,194
  • 2
  • 25
  • 27