Join arrays in VB.NET




.net-3.5 (7)

.NET 3.5 introduced the HashSet class which could do this:

IEnumerable<string> mergedDistinctList = new HashSet<string>(list1).Union(list2);

Not sure of performance, but it should beat the Linq example you gave.

EDIT: I stand corrected. The lazy implementation of Concat and Distinct have a key memory AND speed advantage. Concat/Distinct is about 10% faster, and saves multiple copies of data.

I confirmed through code:

Setting up arrays of 3000000 strings overlapping by 300000
Starting Hashset...
HashSet: 00:00:02.8237616
Starting Concat/Distinct...
Concat/Distinct: 00:00:02.5629681

is the output of:

        int num = 3000000;
        int num10Pct = (int)(num / 10);

        Console.WriteLine(String.Format("Setting up arrays of {0} strings overlapping by {1}", num, num10Pct));
        string[] list1 = Enumerable.Range(1, num).Select((a) => a.ToString()).ToArray();
        string[] list2 = Enumerable.Range(num - num10Pct, num + num10Pct).Select((a) => a.ToString()).ToArray();

        Console.WriteLine("Starting Hashset...");
        Stopwatch sw = new Stopwatch();
        sw.Start();
        string[] merged = new HashSet<string>(list1).Union(list2).ToArray();
        sw.Stop();
        Console.WriteLine("HashSet: " + sw.Elapsed);

        Console.WriteLine("Starting Concat/Distinct...");
        sw.Reset();
        sw.Start();
        string[] merged2 = list1.Concat(list2).Distinct().ToArray();
        sw.Stop();
        Console.WriteLine("Concat/Distinct: " + sw.Elapsed);

This question already has an answer here:

What's the simplest way to join one or more arrays (or ArrayLists) in Visual Basic?

I'm using .NET 3.5, if that matters much.


I think you can use this (I cout name and lastname together for myself and it's not important):

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int k = 0;
string name[20] = /*{ some thing}*/;
string lastname[20] = /*{ some thing}*/;
string database[400];
for(int i = 0 ; i < 20 ; i++ )
{
    for (int j = 0 ; j < 20 ; j++ )
    {
        database[k] = name[i] + " " + lastname[j];
        k++;
        cout<<database<<endl;
    }
}
return 0;
}

you can go to this link and learn more about string for example (operator part) http://www.cplusplus.com/reference/string/string/ and see also http://www.cplusplus.com/doc/tutorial/arrays/ for array


This is in C#, but surely you can figure it out...

int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 6, 7, 8, 9, 10 };
int[] c = a.Union(b).ToArray();

It will be more efficient if instead of calling "ToArray" after the union, if you use the IEnumerable given instead.

int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 6, 7, 8, 9, 10 };
IEnumerable<int> c = a.Union(b);

Use LINQ:

var arr1 = new[] { 1, 2, 3, 4, 5 };
var arr2 = new[] { 6, 7, 8, 9, 0 };
var arr = arr1.Union(arr2).ToArray();


Combine two string arrays with each other

Maybe you can be more specific? You don't even say which language you are writing in..

Also "googling" could help you quit a lot. I would like to give you some tips where to find it etc, but I don't know the language you want want so..

I have come up with these links for you, just searching blind here...

If you are using Perl : How to merge two arrays, alternating values from each array, in perl

Java: Join two arrays in Java?

vb.net: Join arrays in VB.NET

Clean up your question and try it again ;)


Merging two arrays in .NET

If you can manipulate one of the arrays, you can resize it before performing the copy:

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
int array1OriginalLength = array1.Length;
Array.Resize<T>(ref array1, array1OriginalLength + array2.Length);
Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);

Otherwise, you can make a new array

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, newArray, array1.Length);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);

More on available Array methods on MSDN.





.net-3.5