我试图弄清楚如何使用JavaScript ex计算数组中2个重复数字之间的距离:
["2","3","4","2"] => 3 (2 is a duplicate number)
["1","4","5","5"] => 1 (5 is a duplicate number)
["1","4","5","9","4"] => 3 (4 is a duplicate number)
答案 0 :(得分:1)
假设您的数组中任何给定值的出现次数不会超过两次,我们可以采取以下步骤: -
encountered
数组中,这有助于您确定是否已搜索该项目,在Javascript中,以下代码将为您执行相同的操作。
var temp = [2,3,4,2,5,4,1,3];
var encountered = [];
//This function gets you all the indexes of `val` inside `arr` array.
function getAllIndexes(arr, val) {
var indexes = [], i;
for(i = 0; i < arr.length; i++)
if (arr[i] === val)
indexes.push(i);
return indexes;
}
for(var i=0;i<temp.length;i++) {
if(encountered[temp[i]]) continue;
else {
var indexes = getAllIndexes(temp, temp[i]);
encountered[temp[i]] = true;
if(indexes.length>1) {
var steps = indexes[1]-indexes[0];
$('.container').append('Duplicate item: '+temp[i]+' steps: '+ steps+'<br/>');
}
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div class="container"></div>
&#13;
在这里工作JSFiddle - &gt; https://jsfiddle.net/dmnuwas5/
答案 1 :(得分:1)
希望这个伪代码能让你走上正确的道路:
values = original array
distances = {}
firstIndex = {}
loop over values with index i {
item = values[i]
if firstIndex[item] >= 0 { // this is important because the first index could be 0
if item does not exist in distances object { // This would be different if you are going for max distance instead of min distance in the case of multiple of the same item
distances[item] = i - firstIndex[item]
}
} else {
firstIndex[item] = i
}
}
答案 2 :(得分:1)
这是多次出现同一项目并计算至少最小距离的建议。
function getDistance(array) {
var o = {}, r = {};
array.forEach(function (a, i) {
o[a] = o[a] || [];
o[a].push(i);
});
Object.keys(o).forEach(function (k) {
if (o[k].length > 1) {
r[k] = Math.min.apply(null, o[k].reduce(function (r, a, i, aa) {
i && r.push(a - aa[i - 1]);
return r;
}, []));
}
});
return r;
}
document.write('<pre>' + JSON.stringify(getDistance([2,3,4,2,5,4,1,3]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(getDistance(["2", "3", "4", "2", "3", "5", "6", "2"]), 0, 4) + '</pre>');
&#13;
答案 3 :(得分:1)
满足您要求的最小示例。它返回一个对象,其中重复的数字作为键,距离为值。
function sift(arr) {
for (var i = 0, l = arr.length, out = {}; i < l; i++) {
var key = arr.shift();
var index = arr.indexOf(key);
if (index > -1 && !out[key]) out[key] = index + 1;
}
return out;
}
sift(["2","3","4","2"]); // { 2: 3 }
sift(["1","4","5","5"]); // { 5: 1 }
sift(["1","4","5","9","4"]); // { 4: 3 }
sift(["2","3","4","2","3","5","6","2"]); // { 2: 3, 3: 3 }
sift(["6","2","3","4","2","3","5","6","2"]); // { 2: 3, 3: 3, 6: 7 }
答案 4 :(得分:0)
用于查找重复元素之间距离的Java代码。如果存在多个重复元素,则将返回重复元素对之间的最小距离。此外,假设重复元素出现两次(不超过两次)
时间复杂度:O(n)
空间复杂度:O(n)
public int minDuplicateDistance(int[] arr) {
Map<Integer, Integer> dups = new HashMap<Integer, Integer>();
int minDistance = Integer.MAX_VALUE;
for (int x = 0; x < arr.length; x++) {
if (dups.containsKey(arr[x])) {
int index = dups.get(arr[x]);
int distance = x - index;
minDistance = minDistance > distance ? distance : minDistance;
} else {
dups.put(arr[x], x);
}
}
return minDistance;
}