@lillallol/dic
Version:
My own dependency injection container.
30 lines (29 loc) • 834 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIndexOfFirstDuplicateValueOfArray = void 0;
/**
* @description
* It returns the index of the first duplicate value it encounters in the array.
* It returns `-1` when the array has no duplicates.
*
* - time complexity O(n)
* - space complexity O(n)
*
* where n is the length of the provided array.
*/
function getIndexOfFirstDuplicateValueOfArray(_) {
const { array } = _;
const map = new Map();
for (let i = 0; i < array.length; i++) {
const v = map.get(array[i]);
if (v === undefined) {
map.set(array[i], i);
continue;
}
else {
return v;
}
}
return -1;
}
exports.getIndexOfFirstDuplicateValueOfArray = getIndexOfFirstDuplicateValueOfArray;