我有两个字符串数组,但出于演示目的,我将使用字母a-e:
{A,B,C,d,E}
{A,A,B,C,C,C,d,E,E}
我想将第一个数组中每个元素的实例计算为第二个数组中的元素,并返回计数的第三个数组(具有第一个数组的长度)。
例如,给定上面的两个数组,我的程序应返回:
{2,1,3,1,2}
或者,获取第二个数组的方法:
{A,A,B,C,C,C,d,E,E}
并返回:
{A,B,C,d,E}
{2,1,3,1,2}
也会满足我的计划目的。
我在JavaScript中知道我可以这样做:
function foo(arr) {
var a = [], b = [], prev;
arr.sort();
for ( var i = 0; i < arr.length; i++ ) {
if ( arr[i] !== prev ) {
a.push(arr[i]);
b.push(1);
} else {
b[b.length-1]++;
}
prev = arr[i];
}
return [a, b];
}
我如何在Visual Basic中执行此操作?
答案 0 :(得分:0)
我会这样做(直截了当):
Dim strings() As String = {"a","b","c","d","e"}
Dim stringsToCheck() As String = {"a","a","b","c","c","c","d","e","e"}
Dim results(strings.length) as Integer
For Each strToCheck as String in stringsToCheck
For Each str As String in strings
If strToCheck.Equals(str) Then
results(strings.IndexOf(str)) += 1
Exit For
End If
Next
Next