Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数组中两个重复数字之间的距离_Javascript - Fatal编程技术网

Javascript 数组中两个重复数字之间的距离

Javascript 数组中两个重复数字之间的距离,javascript,Javascript,我试图找出如何使用JavaScript ex计算数组中两个重复数字之间的距离: ["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) 用于查找重复元素之间距离的Java代码。如果存在多个重复元素,则将返回重复

我试图找出如何使用JavaScript ex计算数组中两个重复数字之间的距离:

    ["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)

用于查找重复元素之间距离的Java代码。如果存在多个重复元素,则将返回重复元素对之间的最小距离。此外,假设重复元素出现两次(不超过两次)
时间复杂度:O(n)
空间复杂度:O(n)

public int mindUpplicateDistance(int[]arr){
Map dups=new HashMap();
int minDistance=Integer.MAX_值;
对于(int x=0;x距离?距离:距离;
}否则{
重复放置(arr[x],x);
}
}
回心转意;
}

假设数组中任何给定值的出现次数不超过两次,我们可以采取以下步骤:-

  • 遍历数组项并获取每个项的所有索引
  • 将项目添加到遇到的
    数组中,该数组将帮助您确定是否已搜索该项目
  • 如果项目有两次出现,则获取索引之间的差异,这将是您的步骤数
  • 在Javascript中,以下代码将为您执行相同的操作

    var-temp=[2,3,4,2,5,4,1,3];
    遇到的风险值=[];
    //此函数用于获取`arr`数组中`val`的所有索引。
    函数getAllIndex(arr、val){
    var指数=[],i;
    对于(i=0;i');
    }
    }
    }

    希望这个伪代码能让您走上正确的道路:

    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
      }
    }
    

    这是一个关于同一项目多次出现和计算至少最小距离的建议

    函数getDistance(数组){ 变量o={},r={}; forEach(函数(a,i){ o[a]=o[a]| |[]; o[a]。推(i); }); Object.keys(o).forEach(函数(k){ 如果(o[k]。长度>1){ r[k]=Math.min.apply(null,o[k].reduce(函数(r,a,i,aa)){ i&r.push(a-aa[i-1]); 返回r; }, [])); } }); 返回r; } document.write(“”+JSON.stringify(getDistance([2,3,4,2,5,4,1,3]),0,4)+’;
    document.write(“”+JSON.stringify(getDistance([“2”、“3”、“4”、“2”、“3”、“5”、“6”、“2”)),0,4)+’满足您需求的最小示例。它返回一个对象,以重复的数字作为键,以距离作为值

    功能筛选(arr){
    for(var i=0,l=arr.length,out={};i-1&&!out[key])out[key]=index+1;
    }
    返回;
    }
    筛选([“2”、“3”、“4”、“2”]);//{ 2: 3 }
    筛选([“1”、“4”、“5”、“5”]);//{ 5: 1 }
    筛选([“1”、“4”、“5”、“9”、“4”);//{ 4: 3 }
    筛选([“2”、“3”、“4”、“2”、“3”、“5”、“6”、“2”);//{ 2: 3, 3: 3 }
    筛选([“6”、“2”、“3”、“4”、“2”、“3”、“5”、“6”、“2”);//{ 2: 3, 3: 3, 6: 7 }
    

    如果您可以在两种情况下使用,则只保证一个副本或?个副本。那太好了你自己试过吗?或者你只是在寻找答案。如果你能自己发布一些代码来尝试它会更好。我只知道如何使用循环,但我不知道如何使用。@tamnguyen,如果数组中有三个或更多的重复项,我应该得到什么距离值,例如
    [“2”、“3”、“4”、“2”、“3”、“5”、“6”、“2”]
    ?@tamnguyen欢迎!如果这有帮助,请接受。;)
    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
      }
    }
    
    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 }