shawn

shawn

  • NA
  • 1
  • 0

Help with counting individual values in an array

May 28 2008 1:17 PM

Good morning,

I am trying to count how many times each value appears within a given array.  For example is myArray contains 1,1,1,2,2,3,3,3 I need to loop through and count how many times 1,2,3 appear in the array.  The issue I am having is I am not quite sure about the most logical way to do this. 

I have tried for and foreach loops but the problem i run into is looping through I am just countiously adding to my counter variable.

Here is the latest try at this problem. I realize I am way off but any guidance to get on the right track would be greatly appreciate.

int [] myArray = { 1,1,1,2,2,2,3,3};

Array.sort(myArray);

int i = 0;

int counter =0;

foreach(int array in myArray)

{

   if (array ==myArray[i])

{

counter++;

Console.Writeline(array = " is displayed " + counter + " number of times");

i++;

}

else

{

i++

}

}


Answers (1)