-1

So i have 2 arrays like this:

array1 = {1,2,3,4};
array2 = {353, 588, 213, 353};

The numbers in array1 corresponds to the respective indexes in array2, so from this it means: 1 of 353, 2 of 588, 3 of 213, 4 of 353. I want to be able to merge the duplicates in array 2 so the final one becomes:

array1 = {5,2,3};
array2 = {353,588,213};

I was thinking about looping through but I'm thinking it's inefficient so I was hoping someone would know the most efficient way to do this.

Thanks in advance!

Here's another example

array1 = {14,2,3,8,10,2,7,9};
array2 = {353, 588, 353, 213, 588, 213, 200, 353};

array1 = {26,12,10,7};
array2 = {353,588,213,200};
5
  • 1
    so you want to merge the last to the previous occurence, and likewise add up the indexes (indices?) of in array one to the first occurence?
    – Marco
    Commented May 8, 2015 at 7:29
  • 1
    why the first number in converted array1 is 5? Commented May 8, 2015 at 7:31
  • @General-Doomer it seems to be the indexes sum from the first array
    – Banana
    Commented May 8, 2015 at 7:32
  • Because from array2, 353 appears at the first and last, which corresponding to array1, is 1 and 4. 1+4 = 5, so the total number of times is 5 of 353
    – User13888
    Commented May 8, 2015 at 7:32
  • This is not based on indices. the numbers in array1 purely indicates how many of each things in array2 appears. I've added another example
    – User13888
    Commented May 8, 2015 at 7:36

4 Answers 4

8

You can use a diccionary where the key is the array2's value and dictionary's value is the count:

var dic = new Dictionary<int, int>();
for(int i = 0; i < array2.Length; i++) {
    int index = array2[i];
    if(dic.ContainsKey(index)) {
        dic[index] += array1[i];
    } else {
        dic.Add(index, array1[i]);
    }
}

Then, to convert the dictionary to arrays again you only need to iterate over it:

array1 = new int[dic.Count];
array2 = new int[dic.Count];

int position = 0;

foreach (var kvp in dic)
{
    array1[position] = kvp.Value;
    array2[position] = kvp.Key;
    position++;
}

Demo

2
  • 1
    Thank you, how would I convert this dictionary back into the 2 arrays?
    – User13888
    Commented May 8, 2015 at 7:42
  • Answer updated with the conversion and a runnable demo Commented May 8, 2015 at 7:55
4

demo

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var array1 = new int[]{1,2,3,4};
        var array2 = new int[]{353, 588, 213, 353};

        var result = array1.Zip(array2, (a1, a2)=>new {Index = a1, Value = a2})
                           .GroupBy(pair=>pair.Value).ToList();

        array2 = result.Select(gr=>gr.Key).ToArray();
        array1 = result.Select(gr=>gr.Sum(pair=>pair.Index)).ToArray();

        Console.WriteLine(String.Join(", ", array1));       
        Console.WriteLine(String.Join(", ", array2));
    }
}
2

You can use LINQ to get merged data. It's easy to do with Enumerable.Zip method which produce sequence of results from corresponding elements in two sequences (array1 and array2 in your case). It will produce you pairs of elemens:

{ a1 = 1, a2 = 353 }
{ a1 = 2, a2 = 213 }
{ a1 = 3, a2 = 588 }
{ a1 = 4, a2 = 353 }

Next step is grouping with Enumerable.GroupBy by a2. It will produce you groups of pairs

{ Key = 353, [{ a1 = 1, a2 = 353 }, { a1 = 4, a2 = 353 }] }
{ Key = 213, [{ a1 = 2, a2 = 213 }] }
{ Key = 588, [{ a1 = 3, a2 = 588 }] }

Then just select what you need from each group and put results to appropriate arrays:

var groups = array1.Zip(array2, (a1,a2) => new { a1, a2 })
                   .GroupBy(x => x.a2)
                   .ToArray();

array1 = groups.Select(g => g.Sum(x => x.a1)).ToArray();
array2 = groups.Select(g => g.Key).ToArray();
1
int[] array1 = new int[] { 14, 2, 3, 8, 10, 2, 7, 9 };
int[] array2 = new int[] { 353, 588, 353, 213, 588, 213, 200, 353 };

// merge
Dictionary<int, int> dic = new Dictionary<int, int>();
int count = array1.Length;
int sum;
for (int i = 0; i < count; i++)
{
    int a = array1[i];
    int b = array2[i];
    dic[b] = a + (dic.TryGetValue(b, out sum) ? sum : 0);
}

int[] convertedArray1 = dic.Values.ToArray();
int[] convertedArray2 = dic.Keys.ToArray();

// result is:
// convertedArray1 = { 26, 12, 10, 7 }
// convertedArray2 = { 353, 588, 213, 200 }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.