我在AutoHotkey中有array个字符串,其中包含重复的条目。
int getEnter(int measurements[COLUMN][LENGTH]){
int x;
for(x=0;x<LENGTH;x++){
printf("Enter number #%d: ", x+1);
scanf("%d", &measurements[COLUMN][x]);
if(measurements[COLUMN][x]==0){
break;
}
}
return x;
}
void nrOfMeasurements(){
int measurements_count = getEnter(measurements, LENGTH);
return;
}
我想删除所有重复项,以便只保留唯一值。
nameArray := ["Chris","Joe","Marcy","Chris","Elina","Timothy","Joe"]
理想情况下,我正在寻找一个类似于Trim()的函数,它会返回一个修剪过的数组,同时保留原始数组的完整性。 (即trimmedArray := ["Chris","Joe","Marcy","Elina","Timothy"]
)
如何从AutoHotkey数组中删除重复项?
答案 0 :(得分:2)
生成仅包含另一个数组的唯一元素的数组
char c = text.charAt(lastIndex);
此代码使用关联数组来消除重复项。因为它使用键控查找,所以它应该在大型数组上比使用嵌套循环执行得更好,这是 O(n²)
<强>测试强>
uniq(nameArray)
{
hash := {}
for i, name in nameArray
hash[name] := null
trimmedArray := []
for name, dummy in hash
trimmedArray.Insert(name)
return trimmedArray
}
<强>输出强>
请注意,不保留第一个数组中元素的顺序
答案 1 :(得分:1)
试试这个
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)
答案 2 :(得分:1)
保留原始原封,仅循环一次,保留顺序:
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)
}