Ken Young Ken Young -4 years ago 103
C# Question

Counting times values occured in Array

I am having a hard time with a program I need to write. The requirements are that the user enters in the size of the Array. Then they enter the elements of the array after this the program needs to display the values entered then how many times each value occurs. While everything seems to work except its not display the "occurs" part properly. Lets say "1 1 2 3 4" is entered (Array size being 5) It prints

1 occurs 1 time.
1 occurs 1 time.
2 occurs 1 time.
3 occurs 1 time.
4 occurs 2 times.


this isn't right because 1 occured 2 times and 4 is only 1 time. Please Help...

static void Main(string[] args)
{
int[] arr = new int[30];
int size,
count=0,
count1=0,
count2=0;

Console.Write("Enter Size of the Array: ");
size = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the elements of an Array: ");
for (int i=0; i < size; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}

Console.Write("Values Entered: \n");
for (int i = 0; i < size; i++)
{
Console.WriteLine(arr[i]);
if (arr[i] <= 10)
count++;
else
count1++;
}

for(int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (arr[i] == arr[j])
count2++;
else
count2 = 1;
}

Console.WriteLine(arr[i] + " Occurs " + count2 + " Times.");
}
Console.WriteLine("The number of valid entries are: " + count + "\nThe number of invalid entries are: " + count1);
Console.ReadKey();
}

Answer Source

if you can use Linq, this is easy job:

After

Console.Write("Values Entered: \n");

add

var grouped = arr.GroupBy(x => x);
foreach (var group in grouped)
{
    Console.WriteLine("number {0} occurs {1} times", group.Key, group.Count());
}

EDIT

Since OP isn't allowed to use Linq, here's array-only solution. Much more code than that dictionary approach, but with arrays only.

Console.Write("Values Entered: \n");
//an array to hold numbers that are already processed/counted. Inital length is as same as original array's
int[] doneNumbers = new int[arr.Length];
//counter for processed numbers
int doneCount = 0;
//first loop
foreach (var element in arr)
{
    //flag to skip already processed number 
    bool skip = false;
    //check if current number is already in "done" array
    foreach (int i in doneNumbers)
    {
        //it is!
        if (i == element)
        {
            //set skip flag
            skip = true;
            break;
        }
    }

    //this number is already processed, exit loop to go to next one
    if (skip)
        continue;

    //it hasn't been processed yes, so go through another loop to count occurrences
    int occursCounter = 0;
    foreach (var element2 in arr)
    {
        if (element2 == element)
            occursCounter++;
    }

    //number is processed, add it to "done" list and increase "done" counter
    doneNumbers[doneCount] = element;
    doneCount++;

    Console.WriteLine("number {0} occurs {1} times", element, occursCounter);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download