My array is A = {2, 3, 4, 3, 4, 2, 4, 2, 4}
I need an array B that stock at the index i
the number of occurences of i
in the a
If you want to find out how many time each item presents in the, say, array, you can use Linq:
int[] a = new int[]
{ 2, 3, 4, 3, 4, 2, 4, 2, 4 };
// I'd rather not used array, as you suggested, but dictionary
Dictionary<int, int> b = a
.GroupBy(item => item)
.ToDictionary(item => item.Key, item => item.Count());
...
the outcome is
b[2] == 3;
b[3] == 2;
b[4] == 4;
Check out this Fiddle
Or if you don't fancy clicking the link:
var a = new int[]{2, 3, 4, 3, 4, 2, 4, 2, 4};
var b = new int[a.Length];
var aAsList = a.ToList();
for (var i = 0;i < b.Length; i++)
{
var result = aAsList.Count(x=> x == i);
b[i] = result;
if (result != 0)
{
Console.WriteLine(string.Format("b[{0}] : {1}",i,result));
}
}
Your task can be easily accomplished using a Dictionary.
Here is the code :
Dictionary<int, int> ItemCount = new Dictionary<int, int>();
int[] items = { 2, 3, 4, 3, 4, 2, 4, 2, 4 };
foreach (int item in items)
{
if (ItemCount.ContainsKey(item))
{
ItemCount[item]++;
}
else {
ItemCount.Add(item,1);
}
}
Console.WriteLine("A|B");
foreach (KeyValuePair<int,int> res in ItemCount)
{
Console.WriteLine(res.Key +"|"+res.Value);
}
output :
A | B
2 | 3
3 | 3
4 | 4
Note : I think this might be too advance for you , but it's an easy way
Without Dictionary, (A primitive approach)
int[] A = { 2, 3, 4, 3, 4, 2, 4, 2, 4 };
List<int> B = new List<int>(); // <= We need this to check already counted numbers in array
int temp = 0; // <= A temporary variable to get a count per a specific elemet
int count = 0; // < = Will hold number of elements we have already counted
Console.WriteLine("A|B");
for (int i = 0; i < A.Length; i++)
{
temp = 0;
// Check for a fresh number
if (!B.Contains(A[i]))
{
B.Add(A[i]);
// For each element we try to count the number of occurrence
for (int j = 0; j < A.Length; j++)
{
// Current element i matched with a element in array; counts increased
if (A[i] == A[j])
{
temp++; // < = Local count
count++; // <= Kind of the global count of elements we have passed
}
}
Console.WriteLine(A[i] + "|" + temp);
}
// We need to do this only for unique elements; when we have counted all elements in Array A we are done
if (count >= A.Length)
{
break;
}
}
If you are looking for number of occurences, I have made some examples, still I'm not really sure what do you mean by saying if you add arrays in A they should appear in B. To have this kind of funcionality you have to use some notification framework and cannot have simple arrays. At the very least you should wrap all functions where you want to add an element to A and make calculation like i have showed in third example (with result array E).
int[] a = new int[] { 2, 3, 4, 3, 4, 2, 4, 2, 4 };
//version 1 - unsorted array
//find top number of A array
int max_number_a = a.Max() + 1;
//initialize B,C of that size
int[] b = new int[max_number_a]; //RESULT linq version
int[] c = new int[max_number_a]; //RESULT double loop version
for (int i = 0; i < max_number_a; i++)
{
//this is linq way
b[i] = a.Where(x => x == i).Count();
//this is double loop way
c[i] = 0; //initialize position so we can later count/increment when we find each instance of i inside A array
for (int j = 0; j < a.Length; j++)
{
if (a[j] == i) //check if a[j] is the number we are searching for
c[i]++; //we have found one instance of J number, increase the B[i]
}
}
//version 2 - sorted array
int[] d = new int[max_number_a]; //RESULT sorted array
//initialize all to zero
for (int i = 0; i < max_number_a; i++) d[i] = 0; //initialize array to zeros so we can count
List<int> aList = a.OrderBy(x => x).ToList(); //this is linq ordering, choose any other way to order it
while (aList.Count > 0) // we have to use all a elements
{
d[aList[0]]++;
aList.RemoveAt(0);
}
//version 3 - the simple (right) way, and probably what you should be doing :)
int[] e = new int[max_number_a];
//initialize all to zero
for (int i = 0; i < max_number_a; i++) e[i] = 0; //initialize array to zeros so we can count
for (int i = 0; i < a.Length; i++)
{
//we take e index of a[i] and increments its value
e[a[i]]++;
/*
* int number_in_a = a[i];
* int e_index_value = e[number_in_a];
* e[number_in_a] = e_index_value + 1;
*/
}
This seems to be a homework or a tutorial. You have good solutions in Linq, but here is a simple version with basic algorithm:
static void Main(string[] args)
{
int [] A = new int[4];
// you should determine the size of B dynamically here...
// Try to find yourself!
int [] B = new int[999];
/* Code forgotten : initialize array B to 0s */
for (int i = 0; i < A.Length; i++)
{
int item = A[i];
// increase the number at index item
B[item]++;
}
}