Array Duplicate Output Is Repeating

As an assignment, we had to input an array of numbers. Then, we had to output the number and the number of times it was repeated. Although my program does so, it repeats the array element count.


Expected output:

This program counts duplicates in an array of numbers. 
Enter up to 50 numbers and end when done 
1
1
2
3
end
Number		Count
1		2
2		1
3		1
Program ended with exit code: 0


My output:

This program counts duplicates in an array of numbers. 
Enter up to 50 numbers and end when done 
1
1
2
3
end
Number		Count
1		2
1		2
2		1
3		1
Program ended with exit code: 0


The output repeats the count of 1 twice.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void countDuplicates (double array[], int count);
bool validateNum (string validation);
bool myFunction (double i, double j) { return (i < j); }

struct myclass
{
    bool operator() (int i,int j)
    {
        return (i < j);
    }
} myobject;

int main()
{
    double array[50];
    int count = 0;
    string str;
    
    cout << "This program counts duplicates in an array of numbers. " << endl;
    cout << "Enter up to 50 numbers and end when done " << endl;
    
    for (; count < 50; count++)
    {
        cin >> str;
        
        if (str == "end")
            break;
        
        if (validateNum(str))
        {
            cout << "The numbers you input contained letters. Please enter" 
                 << "the numbers again: " << endl;
            count = 0;
            continue;
        }
        
        array[count] = stod(str);
    }
    
    countDuplicates(array, count);
}

void countDuplicates (double array[], int count)
{
    int duplicateCount[count];
    
    vector <double> myvector (array, array + count);
    
    sort (myvector.begin(), myvector.end(), myobject);
    
    for (int i = 0; i < count; i++)
        array[i] = myvector[i];
    
    for (int i = 0; i < count; i++)
        duplicateCount[i] = 0;
    
    for (int i = 0; i < count; i++)
        duplicateCount[i] = std::count(array, array + count, array[i]);
    
    cout << "Number\t\tCount" << endl;
    
    for (int i = 0; i < count; i++)
        cout << array[i] << "\t\t" << duplicateCount[i] << endl;
}

bool validateNum (string validation)
{
    int i = 0, decimal = 0;
    
    for (; i < validation.size(); i++)
    {
        if (!isnumber(validation[i]))
        {
            if (validation[i] == '.')
                decimal++;
            
            if (decimal == 2)
                return true;
            
            else
                return true;
        }
    }
    
    return false;
}


I have worked on this problem for a while now and would really appreciate it if anyone can help me.

Thanks,
VX
Since the final destination of the numbers is std::vector<double> why bother passing them to a C-style array first?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# include <iostream>
# include <sstream>
# include <vector>
# include <string>
# include <cctype>
# include <algorithm>
# include <unordered_map>

constexpr auto SIZE = 5;

int main()
{
    std::vector<double> myVec{};
    for (size_t i = 0; i < SIZE; ++i)
    {
        bool acceptInput{false};
        while (!acceptInput)
        {
            std::cout << "Enter number " << i + 1 << " of " << SIZE << ":\n";
            std::string num{};
            getline(std::cin, num);
            if(std::find_if(num.begin(), num.end(), ::isalpha) != num.end())
            {
                std::cout << "input contains letters, try again \n";

            }
            else
            {
                std::istringstream stream{num};
                double input;
                if(stream)stream >> input;
                myVec.push_back(input);
                acceptInput = true;
            }
        }
    }
    std::unordered_map<double, size_t> vecCounter{};
    //use std::map if ordering is important, std::unordered_map is faster
    // or you could also sort the vector first

    for (const auto& elem : myVec)
    {
        auto itr = vecCounter.find(elem);
        if (itr != vecCounter.end())
        {
            ++(itr -> second);
        }
        else
        {
            vecCounter[elem] = 1;
        }
    }
    for (const auto& elem : vecCounter)
    {
        std::cout << elem.first << " " << elem.second << "\n";
    }
}
Topic archived. No new replies allowed.