如何从AutoHotkey数组中删除重复项? [英] How do I remove duplicates from an AutoHotkey array?

查看:119
本文介绍了如何从AutoHotkey数组中删除重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AutoHotkey中有一个字符串数组,其中包含重复的条目.

I have an array of strings in AutoHotkey which contains duplicate entries.

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

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

I would like to remove any duplicates so that only unique values remain.

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

理想情况下,我正在寻找类似于 Trim()的函数返回修剪后的数组,同时保留原始数组不变. (即trimmedArray := RemoveDuplicates(nameArray))

Ideally I'm looking for a function similar to Trim() which would return a trimmed array while leaving the original array intact. (i.e. trimmedArray := RemoveDuplicates(nameArray))

如何从AutoHotkey阵列中删除重复项?

推荐答案

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

Leaves the original intact, only loops once, preserves order:

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方法的另一种方法是检查哈希对象中的值.这样可能会更有效,更快捷,但是我将把测试留给您.

An alternative to using the haskey method, would be to check a value in our hash object. This may prove to be more efficient and faster, but I'll leave the testing to you.

trimArray(arr) { ; Hash O(n) 

    hash := {}, newArr := []

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

    return newArr
}

最初我不打算进行测试,但是我既好奇又厌倦了等待OP.结果并不令我感到惊讶:

Initially I wasn't going to test, but I got curious as well as tired of waiting on the OP. The results don't surprise me much:

我们在这里看到的是10,000个测试的平均执行时间,该数字越小,任务的计算速度就越快.明显的赢家是我的脚本变体,没有Haskey方法,但幅度很小!所有其他方法都注定要失败,因为它们不是线性解.

What we are seeing here is the average execution times for 10,000 tests, the lower the number, the faster the task was computed. The clear winner is my script variation without Haskey Method, but only by tiny margin! All in the other methods were doomed, being that they are not linear solutions.

测试代码在这里:

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

这篇关于如何从AutoHotkey数组中删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆