Skip to content

Commit 865b565

Browse files
authored
Create 1848-minimum-distance-to-the-target-element.js
1 parent 2cd6af7 commit 865b565

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @param {number} start
5+
* @return {number}
6+
*/
7+
const getMinDistance = function(nums, target, start) {
8+
let min = Infinity, res = -1
9+
for(let i = 0; i < nums.length; i++) {
10+
if(nums[i] === target) {
11+
if(min > Math.abs(i - start)) {
12+
res = i
13+
min = Math.abs(i - start)
14+
}
15+
}
16+
}
17+
18+
return min
19+
20+
};

0 commit comments

Comments
 (0)