Ken Young
Ken Young

Reputation: 33

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();
    }

Upvotes: 1

Views: 70

Answers (3)

Nino
Nino

Reputation: 7115

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);
}

Upvotes: 1

Rufus L
Rufus L

Reputation: 37070

The problem is that you're resetting count2 to 1 any time you find a mismatch.

What you should do instead is set count2 to 0 in the outer loop (so it basically resets once for each item), and then let the inner loop count all the instances of that number:

// For each item 'i' in the array, count how many other items 'j' are the same
for (int i = 0; i < size; i++)
{
    count2 = 0; // processing a new item, so reset count2 to 0

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

    Console.WriteLine(arr[i] + " occurs " + count2 + " times.");
}

Upvotes: 0

yonisha
yonisha

Reputation: 3096

You can simply use dictionary:

    static void Main(string[] args)
    {
        var dic = new Dictionary<int, int>();

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

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

            int current;
            if (dic.TryGetValue(i, out current))
            {
                dic[val] = current + 1;
            }
            else
            {
                dic.Add(val, 1);
            }
        }

        foreach (int key in dic.Keys)
        {
            Console.WriteLine(key + " Occurs " + dic[key] + " Times.");
        }

        Console.Read();
    }

Upvotes: 0

Related Questions