如何从 AutoHotkey 数组中删除重复项?

How do I remove duplicates from an AutoHotkey array?

我在 AutoHotkey 中有 array 个字符串,其中包含重复条目。

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

我想删除所有重复项,以便只保留唯一值。

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

理想情况下,我正在寻找类似于 Trim() 的函数,它会 return 修剪数组,同时保持原始数组完好无损。 (即 trimmedArray := RemoveDuplicates(nameArray)

如何从我的 AutoHotkey 数组中删除重复项?

试试这个

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)

Check this

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

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)

输出

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

保留原样,只循环一次,保留顺序:

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 方法的替代方法是检查哈希对象中的值。这可能会更有效、更快速,但我会把测试留给你。

trimArray(arr) { ; Hash O(n) 

    hash := {}, newArr := []

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

    return newArr
}

编辑:最初我不打算测试,但我很好奇,也厌倦了等待 OP。结果并不让我吃惊:

我们在这里看到的是 10,000 次测试的平均执行时间,数字越低,任务计算速度越快。明显的赢家是我的不使用 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) 
}