Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function firstDuplicate(a) {
- var duplicateObj = {'lowestIdx': Infinity};
- for (var i = 0; i < a.length; ++i) {
- if (!duplicateObj[a[i]]) {
- duplicateObj[a[i]] = -1;
- } else {
- duplicateObj[a[i]] = i;
- }
- }
- for (var val in duplicateObj) {
- if (val === 'lowestIdx') {
- continue;
- }
- if (duplicateObj.hasOwnProperty(val) && duplicateObj[val] != -1) {
- if (duplicateObj.lowestIdx > duplicateObj[val]) {
- duplicateObj.lowestIdx = duplicateObj[val];
- }
- }
- }
- if (duplicateObj.lowestIdx != Infinity) {
- return a[duplicateObj.lowestIdx];
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement