Finding the most repeated element, and the greatest and smallest values in an array

Finding the most repeated element, and the greatest and smallest values in an array


Hi, this is Mazen. In this post, we're going to solve a programming example, which is three searching operations on an array. First, we introduce two simple approaches to find the most repeated element, that is - the element which has the most count. Next, we elaborate on the logic of finding either of the greatest and smallest elements.

The question says:

Write a program that searches an array for the most repeated number, and the smallest and greatest values

It's a good practice to separate the requirements into subproblems and then putting all of them together to make up the main program. Therefore, we're going to start solving the first part of the question which is finding the most repeated number. To do that, there are two simple approaches. I'm going to show you both of them and it's for you which one to choose.

Before doing the three operations, we assume that you previously declared an integer array, named it "array" and got ten inputs for it.

    int array[10];

    // Getting some numbers to search on
    std::cout << "Enter ten integers:\n";
    for (int i = 0; i < 10; i++)
    {
        std::cout << "Integer #" << i + 1 << ": ";
        std::cin >> array[i];
    }

Finding the most repeated number in an array (using the array of counts approach)

Step 1: Declare an integer array with the size of 10, name it "count", and give the first element of it an initial value of 0.

Note: by initializing the first element of an array, each of the rest elements would be assigned a value of zero.

    int count[10] = { 0 };

Step 2: Create two nested for loops, both of them share the same properties. The initial number is 0, the condition is that the initial number is less than 10, and the initial number is incremented by one for each repetition.

Inside of the body of the loop, we create an if statement to check the count of each element of "array" over a condition of (array[i] == array[j]). When the condition matches, the corresponding element of the array "count" is incremented.

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
{
    if (array[i] == array[j])
        count[i]++;
}
    }

Step 5: Create an integer variable to hold the greatest count, name it greatestCount, and initialize it with the value of the first element of the array "count"

    int greatestCount = count[0];

Step 6: Create a for loop with nine repetitions, starting from 1 all the way up to 9.
Inside of the loop, we use the ternary operator to determine the greatest value of "count" and assign it to greatestCount

    for (int i = 1; i < 10; i++)
greatestCount = count[i] > greatestCount ?
    count[i] : greatestCount;

Step 7: We match the greatest count with its corresponding element of "array":
1) Create an integer variable to hold the element. Name it "mostRepeated" for instance.
2) Create a for loop with ten repetitions to pass through the elements of "count" and "array".
3) Inside of the body of the loop, we pick up the greatest count of the array "count" and then assign its corresponding element of the array "array" to "mostRepeated".

    int mostRepeated;
    for (int i = 0; i < 10; i++)
if (count[i] == greatestCount)
    mostRepeated = array[i];

Step 8: Finally, we output the final result which is mostRepeated

    std::cout << "The most repeated number (using the first method): " 
        << mostRepeated << std::endl;


Finding the most repeated number in an array (using another approach)

Step 1: Declare an integer variable, name it "max_count" and initialize it with a value of zero

    int max_count = 0;

Step 2: Create a for loop with ten repetitions starting from 0 all the way up to 9. Inside of the body of the loop, we determine the max count using the following logic:

1) Declare an integer variable to hold the current count of each element. Give it an initial value of zero.
2) Determine the count of the current element of "array" using a for loop to pass through the elements and an if statement to compare the current element to the rest ones.
3) Before moving on to the next repetition, we must assign current_count to max_count just if current_count is greater than the previous value of max_count.


    for (int i = 0; i < 10; i++)
    {
int current_count = 0;
for (int j = i; j < 10; j++)
    if (array[j] == array[i])
current_count++;

if (current_count > max_count)
    max_count = current_count;
    }

Step 3: Create an integer variable to hold the element of "array" that has the most count. We name this variable "most_repeated" for instance

    int most_repeated;

Step 4: Create a ten-repetition for loop, starting from 0 up to 9. Inside of the body of the loop, we assign max_count to its corresponding element of "array" using the following logic:

1) Declare an integer variable with an initial value of zero. This variable is in charge of holding the count of the current element.
2) Create an inner for loop that compares each element with the rest. The comparison is done by an if statement placed inside of the inner loop.
3) Right after the inner loop, we check whether the count of the current element of "array" is equal to the previously found max count. If true, then this element is the most repeated. Otherwise, continue looping to the next index.

    for (int i = 0; i < 10; i++)
    {
int current_count = 0;
for (int j = i; j < 10; j++)
    if (array[j] == array[i])
        current_count++;

    if (current_count == max_count)
most_repeated = array[i];
    }

Step 5: At the end, we just print the most repeated number on the screen.

    std::cout << "The most repeated number (using the second method): " 
<< most_repeated << std::endl;


Searching for the smallest and greatest values in an array in C++

Because finding either of the smallest or greatest element in an array have the same logic with a little difference, I decided to put them together in one block rather than explaining them separately.

Step 1: Declare two integer variables to hold the smallest and greatest numbers. Both of them take the first element of "array" as an initial value

    int greatestElement = array[0];
    int smallestElement = array[0];

Step 2: Create a for loop with ten repetitions to pass through the indexes of "array". Inside of the body of the loop, we find the greatest and smallest elements using the ternary operator as follows:

- Assign the current element of "array" to greatestElement just if it's greater than greatestElement. If the condition doesn't match, keep the value of greatestElement as is.
- To get the smallest element ,rewrite the previous statement with altering the condition from "greater" to "smaller".

    for (int i = 0; i < 10; i++)
    {
        greatestElement = array[i] > greatestElement
            ? array[i] : greatestElement;

smallestElement = array[i] < smallestElement
    ? array[i] : smallestElement;
    }

Step 3: Output the results of both of the greatest and smallest elements

    std::cout << "The greatest number is: " << greatestElement
<< std::endl;
    std::cout << "The smallest number is: " << smallestElement
<< std::endl;


Putting it all together

  	#include <iostream>
  
  	int main()
        {
            int array[10];

            // Getting some numbers to search on
            std::cout << "Enter ten integers:\n";
            for (int i = 0; i < 10; i++)
            {
                std::cout << "Integer #" < i + 1 < ": ";
                std::cin >> array[i];
            }

            // Searching for the most repeated number
            // Using the array of counts method

            int count[10] = { 0 };

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (array[i] == array[j])
                        count[i]++;
                }
            }

            int greatestCount = count[0];
            for (int i = 1; i < 10; i++)
                greatestCount = count[i] > greatestCount ?
                    count[i] : greatestCount;

            int mostRepeated;
            for (int i = 0; i < 10; i++)
                if (count[i] == greatestCount)
                    mostRepeated = array[i];

            std::cout << "\nThe most repeated number (using the first method): " 
                << mostRepeated << std::endl;

            // Searching for the most repeated number
            // Using another method
            int max_count = 0;
            for (int i = 0; i < 10; i++)
            {
                int current_count = 0;
                for (int j = i; j < 10; j++)
                    if (array[j] == array[i])
                        current_count++;

                if (current_count > max_count)
                    max_count = current_count;
            }

            int most_repeated;
            for (int i = 0; i < 10; i++)
            {
                int current_count = 0;
                for (int j = i; j < 10; j++)
                    if (array[j] == array[i])
                        current_count++;

                if (current_count == max_count)
                    most_repeated = array[i];
            }

            std::cout << "The most repeated number (using the second method): "
                << most_repeated << "\n\n";


            // Searching for the smallest and greatest numbers
            int greatestElement = array[0];
            int smallestElement = array[0];

            for (int i = 0; i < 10; i++)
            {
                greatestElement = array[i] > greatestElement
                    ? array[i] : greatestElement;

                smallestElement = array[i] < smallestElement
                    ? array[i] : smallestElement;
            }

            std::cout << "The greatest number is: " << greatestElement
                << std::endl;
            std::cout << "The smallest number is: " << smallestElement
                << std::endl;


            system("pause");
            return 0;
        }
    

Output

Write a program that searches an array for the most repeated number and the smallest and greatest values


Comments

Popular posts from this blog

Finding (positive - negative - odd - even - zero) numbers in an array

Two methods for reversing a text string in c++

Taking inputs from the user, storing them in a (1D-2D-3D) array and printing them on the screen in c++