How to return "unknown" when there are 2 attributes that have same occurances?

I would like to find out which label occurs most frequently. I first count the number of occurrences of each label. Then compare the occurrences and find out the maximum. But now I need to add an addition requirement: If 2 labels have the same occurances, return label "unknown".
Could anybody help me with the part of "identify which is the most frequent label"? There seems some error in the arrangement of the if statement and for loop, because the output is always different to what is expected.

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
Node *DTL(vector<creditrating> data, int minleaf){
    int A =  labelIsEqual(data);
    if(data.size() <= minleaf || A == 1 ){
        Node *node = new Node();
        //calculate the number of each of the 8 different level of rating AAA,BB...
        int number[8];
        for (int i = 0; i < 8; i++){
            number[i] = 0;
        }
        //go through data
        for (creditrating d: data){
            if( d.Rating == "AAA"){
                number[0] ++;
            }
            else if(d.Rating == "AA"){
                number[1] ++;
            }
            else if(d.Rating == "A"){
                number[2] ++;
            }
            else if(d.Rating == "BBB"){
                number[3] ++;
            }
            else if(d.Rating == "BB"){
                number[4] ++;
            }
            else if(d.Rating == "B"){
                number[5] ++;
            }
            else if(d.Rating == "CCC"){
                number[6] ++;
            }
            else{
                number[7] ++;
            }
        }

//-------------------------identify which is the most frequent label---------------------------------------
        int max = 0;
        int index = 0;

        for (int i = 0; i < 8; i++){
            for (int j = i; j < 8; j++){
                if(number[i] == number[j]){
                    index = 7;
                }else if (number[i] > max){
                    max = number[i];
                    index = i;
                }
            }
        }
        
        string label = "";
        switch (index){
            case 0: 
                label = "AAA";
                break;
            case 1: 
                label = "AA";
                break;
            case 2: 
                label = "A";
                break;      
            case 3: 
                label = "BBB";
                break;
            case 4: 
                label = "BB";
                break;
            case 5: 
                label = "B";
                break;
            case 6: 
                label = "CCC";
                break;
            case 7: 
                label = "unknown";
                break;
            default:
                break;
        }
        node->label = label;
        return node;
    }
Last edited on
index 7 is sticky. Once you've found it once, there's no point carrying on with the search.
1
2
3
4
5
6
7
8
9
10
        for (int i = 0; index < 7 && i < 8; i++){
            for (int j = i; index < 7 && j < 8; j++){
                if(number[i] == number[j]){
                    index = 7;
                }else if (number[i] > max){
                    max = number[i];
                    index = i;
                }
            }
        }
Topic archived. No new replies allowed.