Michael
Michael

Reputation: 13626

How to count how many times exist each number from int[] inside IEnumerable<int>?

I have array of ints(Call him A) and IEnumarable(Call him B):

B - 1,2,4,8,289

A - 2,2,56,2,4,33,4,1,8,

I need to count how many times exist each number from A inside B and sum the result.

For example:

B - 1,2,4,8,289

A - 2,2,56,2,4,33,4,1,8,

result = 1+3+2+1+0

What is elegant way to implement it?

Upvotes: 2

Views: 402

Answers (5)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112512

With LINQ it is easy:

int count = A
    .Where(x => B.Contains(x))
    .Count();

Counts how many times elements from A are contained in B.


As Yuval Itzchakov points out, this can be simplified like this:

int count = A.Count(x => B.Contains(x));

Upvotes: 4

Stilgar
Stilgar

Reputation: 23571

It is not clear if you want to know the occurrences of each number or the final count (your text and your example code differ). Here is the code to get the number of appearances of each number

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

public class Program
{
    public static void Main()
    {
        int[] a = new []{1,2,3};
        int[] b = new []{1,2,2,3};
        Dictionary<int, int> aDictionary = a.ToDictionary(i=>i, i => 0);

        foreach(int i in b)
        {
            if(aDictionary.ContainsKey(i))
            {
                aDictionary[i]++;
            }
        }

        foreach(KeyValuePair<int, int> kvp in aDictionary)
        {
            Console.WriteLine(kvp.Key + ":" + kvp.Value);
        }
    }
}

Upvotes: 1

Arghya C
Arghya C

Reputation: 10078

With Linq you can do like this

var B = new List<int>{ 1, 2, 4, 8, 289 };
var A = new List<int> { 2, 2, 56, 2, 4, 33, 4, 1, 8 };

var repetitionSum = B.Select(b => A.Count(a => a == b)).Sum(); //result = 7

And if you want, you can get the individual repetition list like this

var repetition = B.Select(b => A.Count(a => a == b)).ToList();
// { 1, 3, 2, 1, 0 }

Upvotes: 1

Deepak Sharma
Deepak Sharma

Reputation: 4170

sweet and simple.. one line solution

why dont you try it..

        int sum = 0;
        A.ToList().ForEach(a=>sum +=B.Count(b=>b==a));
        Console.Write(sum);

you can sweap the A/B it will still work

Upvotes: 1

Eric J.
Eric J.

Reputation: 150138

I need to count how many times exist each number from A inside B and sum the result.

You can get both the count and sum as follows

List<int> b = new List<int>() { 1,2,4,8,289 };
List<int> a = new List<int>() { 2,2,56,2,4,33,4,1,8 };

var subset = a.Where(i => b.Contains(i));
var count = subset.Count(); // 7
var sum = subset.Sum();  // 23

Note that I reuse the same Linq expression to get both the count and the sum.

One might be tempted to use a HashSet<int> in place of a List<int> because the .Contains operation is faster. However, HashSet is a set, meaning if the same number is added multiple times, only one copy of that number will remain in the set.

Upvotes: 1

Related Questions