Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining/merging arrays in C#

I have two or more arrays -- one with IDs, one or more with string values. I want to merge these into a hash table so I can look up values by ID.

The following function does the job, but a shorter and sweeter version (LINQ?) would be nice:

Dictionary<int, string[]> MergeArrays( IEnumerable<int> idCollection,
                                       params IEnumerable<string>[] valueCollections )
{
    var dict = new Dictionary<int, string[]>();

    var idL = idCollection.Count();
    while ( idL-- > 0 )
    {
        dict[idCollection.ElementAt( idL )] = new string[valueCollections.Length];

        var vL = valueCollections.Length;
        while ( vL-- > 0 )
            dict[idCollection.ElementAt( idL )][vL] = valueCollections[vL].ElementAt( idL );
    }

    return dict;
}

Any ideas?

like image 318
Bergius Avatar asked Feb 13 '09 10:02

Bergius


People also ask

How do I combine two arrays?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

What is merging operation in array?

Take two arrays as input and try to merge or concatenate two arrays and store the result in third array. The logic to merge two arrays is given below − J=0,k=0 for(i=0;i<o;i++) {// merging two arrays if(a[j]<=b[k]){ c[i]=a[j]; j++; } else { c[i]=b[k]; k++; } }

How do I merge two arrays with the same key?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.

Is there a merge function in C?

Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then it merges the two sorted halves. The merge() function is used for merging two halves.


1 Answers

That's very inefficent at the moment - all those calls to ElementAt could be going through the whole sequence (as far as they need to) each time. (It depends on the implementation of the sequence.)

However, I'm not at all sure I even understand what this code is doing (using foreach loops would almost certainly make it clearer, as would iterating forwards instead of backwards. Could you give some sample input? and expected outputs?

EDIT: Okay, I think I see what's going on here; you're effectively pivoting valueCollections. I suspect you'll want something like:

static Dictionary<int, string[]> MergeArrays(
    IEnumerable<int> idCollection,
    params IEnumerable<string>[] valueCollections)
{
    var valueCollectionArrays = valueCollections.Select
         (x => x.ToArray()).ToArray();
    var indexedIds = idCollection.Select((Id, Index) => new { Index, Id });

    return indexedIds.ToDictionary(x => Id, 
        x => valueCollectionArrays.Select(array => array[x.Index]).ToArray());
}

It's pretty ugly though. If you can make idCollection an array to start with, it would frankly be easier.

EDIT: Okay, assuming we can use arrays instead:

static Dictionary<int, string[]> MergeArrays(
    int[] idCollection,
    params string[][] valueCollections)
{
    var ret = new Dictionary<int, string[]>();
    for (int i=0; i < idCollection.Length; i++)
    {
         ret[idCollection[i]] = valueCollections.Select
             (array => array[i]).ToArray();
    }
    return ret;
}

I've corrected (hopefully) a bug in the first version - I was getting confused between which bit of the values was an array and which wasn't. The second version isn't as declarative, but I think it's clearer, personally.

like image 181
Jon Skeet Avatar answered Sep 26 '22 01:09

Jon Skeet