Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
Arrays 如何从自动热键阵列中删除重复项?_Arrays_Duplicates_Autohotkey - Fatal编程技术网

Arrays 如何从自动热键阵列中删除重复项?

Arrays 如何从自动热键阵列中删除重复项?,arrays,duplicates,autohotkey,Arrays,Duplicates,Autohotkey,自动热键中有一组字符串,其中包含重复的条目 nameArray := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"] 我希望删除任何重复项,以便只保留唯一值 trimmedArray := ["Chris","Joe","Marcy","Elina","Timothy"] 理想情况下,我正在寻找一个类似的函数,它将返回一个修剪过的数组,同时保持原始数组不变。(即trimmedArray:=移除的副本(nameArray)) 如何

自动热键中有一组字符串,其中包含重复的条目

nameArray := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"]
我希望删除任何重复项,以便只保留唯一值

trimmedArray := ["Chris","Joe","Marcy","Elina","Timothy"]
理想情况下,我正在寻找一个类似的函数,它将返回一个修剪过的数组,同时保持原始数组不变。(即
trimmedArray:=移除的副本(nameArray)

如何从自动热键阵列中删除重复项?

试试这个

names := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"]

for i, namearray in names
    for j, inner_namearray in names
        if (A_Index > i && namearray = inner_namearray)
            names.Remove(A_Index)

生成一个仅包含另一个数组的唯一元素的数组

uniq(nameArray)
{
  hash := {}
  for i, name in nameArray
    hash[name] := null

  trimmedArray := []
  for name, dummy in hash
    trimmedArray.Insert(name)

  return trimmedArray
}
此代码使用关联数组来消除重复项。由于它使用键控查找,因此它在大型数组上的性能应优于使用嵌套循环,即O(n²)

测试

for i, name in uniq(["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"])
  s := s . ", " . name

MsgBox % substr(s, 3)
输出

for i, name in uniq(["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"])
  s := s . ", " . name

MsgBox % substr(s, 3)


请注意,第一个数组中元素的顺序不保留

保留原始元素的完整性,仅循环一次,保留顺序:

nameArray := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"]

trimmedArray := trimArray(nameArray)

trimArray(arr) { ; Hash O(n) 

    hash := {}, newArr := []

    for e, v in arr
        if (!hash.Haskey(v))
            hash[(v)] := 1, newArr.push(v)

    return newArr
}
使用haskey方法的替代方法是检查hash对象中的值。这可能会被证明更高效、更快,但我将把测试留给您

trimArray(arr) { ; Hash O(n) 

    hash := {}, newArr := []

    for e, v in arr
        if (!hash[v])
            hash[(v)] := 1, newArr.push(v)

    return newArr
}
编辑:起初我不打算测试,但我对等待OP感到好奇和厌倦。结果并不让我太惊讶:

我们在这里看到的是10000个测试的平均执行时间,数量越少,任务的计算速度就越快。明显的赢家是我没有Haskey方法的脚本变体,但只有很小的差距!所有其他方法都是注定的,因为它们不是线性解

测试代码如下:

setbatchlines -1 

tests := {test1:[], test2:[], test3:[], test4:[]}

Loop % 10000 {
    nameArray := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"]

    QPC(1)

    jimU(nameArray)

    test1 := QPC(0), QPC(1)

    AbdullaNilam(nameArray)

    test2 := QPC(0), QPC(1)

    ahkcoderVer1(nameArray)

    test3 := QPC(0), QPC(1)

    ahkcoderVer2(nameArray)

    test4 := QPC(0)

    tests["test1"].push(test1), tests["test2"].push(test2)
    , tests["test3"].push(test3), tests["test4"].push(test4)
}

scripts := ["Jim U         ", "Abdulla Nilam  "
            , "ahkcoder HasKey", "ahkcoder Bool  " ]

for e, testNums in tests ; Averages Results
    r .= "Test Script " scripts[A_index] "`t:`t" sum(testNums) / 10000 "`n"


msgbox % r

AbdullaNilam(names) {

    for i, namearray in names
        for j, inner_namearray in names
            if (A_Index > i && namearray = inner_namearray)
                names.Remove(A_Index)
    return names
}

JimU(nameArray) {
  hash := {}
  for i, name in nameArray
    hash[name] := null

  trimmedArray := []
  for name, dummy in hash
    trimmedArray.Insert(name)

  return trimmedArray
}

ahkcoderVer1(arr) { ; Hash O(n) - Linear

    hash := {}, newArr := []

    for e, v in arr
        if (!hash.Haskey(v))
            hash[(v)] := 1, newArr.push(v)

    return newArr
}

ahkcoderVer2(arr) { ; Hash O(n) - Linear

    hash := {}, newArr := []

    for e, v in arr
        if (!hash[v])
            hash[(v)] := 1, newArr.push(v)

    return newArr
}

sum(arr) {
    r := 0
    for e, v in arr
        r += v
    return r
}

QPC(R := 0) ; https://autohotkey.com/boards/viewtopic.php?t=6413
{
    static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", F)
    return ! DllCall("QueryPerformanceCounter", "Int64P", Q) + (R ? (P := Q) / F : (Q - P) / F) 
}

是否有一种方法可以使原始未经修剪的数组保持原样?(与工作原理类似)@StevenVascellaro您可以在处理之前将其删除。任何阅读代码的人都应该能够确定其目的,并推断初始数组上的单个迭代将比两个更快,正如其他示例所示。我添加了一个供您测试的替代方案,我建议使用一个大型数据集,并以毫秒为单位比较计算时间。我不确定内置的haskey方法是否会增加额外的成本,一个简单的值检查可能会稍微快一点。很好的测试!谢谢分享。虽然您的变体显然更有效,但我相信我们的算法具有相同的复杂性:在平均数据集上操作时为O(n)。我遍历散列的额外步骤应该是O(n),并且不会增加复杂性。是吗?我的Big O研究已经有一段时间了,但我相信通过数据结构的每次迭代都会增加复杂性(即时间),这就是为什么在测试中,您的算法较慢的原因。我不得不说,你差一点就找到了一个完美的解决方案,这也是为什么我跳到这里发布我的解决方案的原因。很久以前我就已经对这个答案投了赞成票(谢谢!),但是你能解释一下你是如何使用
hash[(v)]:=1
?它真的是
1
,还是被认为是
True