Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function firstDuplicate(a) {
  2.     var duplicateObj = {'lowestIdx': Infinity};
  3.    
  4.     for (var i = 0; i < a.length; ++i) {
  5.         if (!duplicateObj[a[i]]) {
  6.             duplicateObj[a[i]] = -1;
  7.         } else {
  8.             duplicateObj[a[i]] = i;
  9.         }
  10.     }
  11.          
  12.     for (var val in duplicateObj) {
  13.         if (val === 'lowestIdx') {
  14.             continue;
  15.         }
  16.        
  17.         if (duplicateObj.hasOwnProperty(val) && duplicateObj[val] != -1) {
  18.             if (duplicateObj.lowestIdx > duplicateObj[val]) {
  19.                 duplicateObj.lowestIdx = duplicateObj[val];
  20.             }
  21.         }
  22.     }
  23.    
  24.     if (duplicateObj.lowestIdx != Infinity) {
  25.         return a[duplicateObj.lowestIdx];
  26.     }
  27.    
  28.     return -1;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement