C ++ Array (excluding the number of repetitions)

I am a beginner programmer and I need help. I need to write a program that reads an array of 10 numbers from the user, then scans it and calculates the most common / s numbers in the array itself and prints them. If there is only one number in the array, print only that number. But, if there is more than one number that appears more than once, print them also in the order in which they appear in the array. For example, 1 2 3 3 4 5 6 7 8 9 - the output will be 3 For 1 2 3 4 1 2 3 4 5 6 - the output will be 1 2 3 4 for-1 1 1 1 1 2 2 2 3 3 4 - the output will be 1 2 3

Now, the problem I am facing is that whenever I have a number that repeats more than two times (see the third example above), the output I get is the number of iterations of the loop for that number and not only this number once. Any help would be appreciated.

The code is attached below -

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int array [10], index, checker, common;
main ()
{

        for (index=0; index<10; index++)
        {
            cin >> array [index];
        }

        for (index=0; index<10; index++)
            {
                int tempcount=0;
                for (checker=(index+1);checker<10;checker++)
                    {   
                        if (array[index]==array[checker])
                            tempcount++;
                    }
                   if (tempcount>=1)
                   cout << array[index]<<" ";

            }

    return 0;
}
+4
source share
7 answers

Use appropriate data structures for the task.

Create a std :: unordered_map that maps the value to number_of_occurrences and takes one pass through the input.

Then create another card with the o_o_valuation number for the value. Sort it in descending order. Report the first value plus any additional that occurred as many times as the first.

0

, , , . :

  • distinct .

, .

EDIT:

int array[] = {1, 2, 3, 4, 1, 2, 3, 4, 5, 6};

int first [11], cnt[11];

for(int i = 0; i < 11; i++){
    first[i] = -1;
    cnt[i] = 0;
}

int max = 0;

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

for(int i = 0; i <= max; i++){
    if(cnt[i] > 1 && first[i] == -1) {
        printf(" %d", i);
        first[i] = i;   
    }
}
0

, , , , , . , maxCount, . , .

, .

0

- . . , , , . , , ( num) 1.

for (int i = 0; i < 10; i++)
{
    int presentBefore = 0;
    for (int j = 0; j < i; j++) //if any previous occurence of element
    {
        if (array[i] == array[j]) presentBefore++;
    }

    if (presentBefore == 0)//if first occurence of the element
    {
        int num = 1;
        for (int j = i + 1; j < 8; j++)// if occurences ahead in the array
        {
          if (array[i] == array[j]) num++;
        }
        if(num>1)cout<<array[i]<<" ";
    }
}
0

, STL std::set.

#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>

int main()
{

    int array[12] = { 1, 2, 3, 1, 2, 4, 5, 6, 3, 4, 1, 2 };
    std::set<int> dupes;

    for (auto it = std::begin(array), end = std::end(array); it != end; ++it)
    {
        if (std::count(it, end, *it) > 1 && dupes.insert(*it).second)
            std::cout << *it << " ";
    }

    return 0;
}

1 2 3 4

, :

  • (BTW, , , 10, )
  • , std::count std::set
  • count > 1, , , set .
  • std::set , , set, .second return false.
  • , , , -, , .
0

class std::vector, , ...

, , , :

1: .

2: 2 array[i] array[j], , element j . j i + 1, .

3 ; 2 .

4- .

  • NB: "< < , .

:

#include <iostream>
#include <vector>


std::ostream& operator << (std::ostream& out, std::vector<int> vecInt){
    for(int i(0); i < vecInt.size(); i++)
        out << vecInt[i] << ", ";
    return out;
}



int main() {

    std::vector< int > vecInt;
    //1 1 1 1 2 2 2 3 3 4 
    vecInt.push_back(1);
    vecInt.push_back(1);
    vecInt.push_back(1);
    vecInt.push_back(1);
    vecInt.push_back(2);
    vecInt.push_back(2);
    vecInt.push_back(2);
    vecInt.push_back(3);
    vecInt.push_back(3);
    vecInt.push_back(4);

    std::vector<int> vecUniq;

    for(int i(0); i < vecInt.size(); i++)
        for(int j(i + 1); j < vecInt.size(); j++)
            if(vecInt[i] == vecInt[j])
                vecUniq.push_back(vecInt[j]);

            std::cout << vecUniq << std::endl;

    for(int i = 0; i < vecUniq.size(); i++)
        for(int j = vecUniq.size() - 1 ; j >= 0 && j > i; j--)
            if(vecUniq[i] == vecUniq[j])
                vecUniq.erase(&vecUniq[j]);

            std::cout << vecUniq << std::endl;


    std::cout << std::endl;
    return 0;

}


The input:    1 2 3 3 4 5 6 7 8 9 
The output:   3

The input:    1 2 3 4 1 2 3 4 5 6
The output:   1 2 3 4

The input:    1 1 1 1 2 2 2 3 3 4
The output:   1 2 3
0

, , , , . :

#include <iostream>
using namespace std;

int print(int a[],int b[])
{
    cout<<"b :: ";
    for (int index=0;index<10;index++)
    {
        cout<<b[index]<<"  ";
    }
    cout<<endl;
}

int main ()
{
    int a[10],b[11], index, checker, common;
        for (index=0; index<10; index++)
        {
            cin >> a [index];
            b[index] = 0;
        }
        b[10] =0;

        for (index=0;index<10;index++)
        {

            b[a[index]]++;
            if (b[a[index]] == 2)
                cout<<a[index];
            //print(a,b);
        }

    return 0;
}

, b , . b , , b 10, b [11], 10. 0 , , , 0.

b 0. , : 1 2 3 4 1 2 3 4 5 6

b , ::

b :: 0  1  0  0  0  0  0  0  0  0     ....1
b :: 0  1  1  0  0  0  0  0  0  0     ....2
b :: 0  1  1  1  0  0  0  0  0  0     ....3
b :: 0  1  1  1  1  0  0  0  0  0     ....4
b :: 0  2  1  1  1  0  0  0  0  0     ....5
b :: 0  2  2  1  1  0  0  0  0  0     ....6
b :: 0  2  2  2  1  0  0  0  0  0     ....7
b :: 0  2  2  2  2  0  0  0  0  0     ....8
b :: 0  2  2  2  2  1  0  0  0  0     ....9
b :: 0  2  2  2  2  1  1  0  0  0     ....10

5 b 1 2, 1, [index].

And the element array will be printed only when it is repeated for the first time because of this line , if (b [a [index]] == 2) .

This program uses the idea of โ€‹โ€‹counting sorting, so if you want, you can check the sorting of counting.

0
source

Source: https://habr.com/ru/post/1689800/


All Articles