-1

I was trying to solve the LeetCode Question:: 1207: Unique Number of Occurrences.

My Way of Thinking:

  1. I will make a structures Occurences which will contain two sub-elements, count,element. Element will store the element while count will store the count of element.
  2. Now I will create a IN function which will check whether whether a element is present in Array of type Occurence.
  3. For count_occurences function, I will check whether Var is present in Array or not. If No, Then I add the element and it's count value to it. If Yes, then we increase the count value.

Code:

#include<iostream>
using namespace std;

typedef struct Occurence{
    int element;
    int count = 0;
}Occurence;

bool IN(Occurence Arr[],int var,int size){
    for(int i=0;i<size;i++){
        if(var == Arr[i].element){
            return true;
        }
        else{
            return false;
        }
    }
    return false;
}

void count_Occurence(Occurence Array[],int Arr[],int size){
    for(int i=0;i<size;i++){
        int Var = Arr[i];
        if( IN(Array,Var,size) ){
            Array[i].count++;
        }else{
            Array[i].element = Var;
            Array[i].count++;
        }
    }
}

int main(){
    int Arr[1000];
    int N;
    cin >> N;
    
    for(int i=0;i<N;i++){
        cin >> Arr[i];
    }

    Occurence *Array = (Occurence*)malloc(N*sizeof(Occurence));

    count_Occurence(Array,Arr,N);
    int ans = 0;
    for(int i=0;i<N;i++){
        ans = ans ^ Array[i].count;
    }
    
    cout << ans;
    return 0;
}

Problem:

  1. I initialized the value of count in structure definition as 0 but it isn't getting initialized as zero when I ran it. It's some garbage value. I am not able to understand why?
  • 3
    `typedef struct ...` - you don't need to `typedef` your classes in C++. They are `typedef`ined automatically. Also: Don't use `malloc`. Use a `std::vector`. – Ted Lyngmo Aug 14 '22 at 11:12
  • 3
    So-called "competition" or "judge" sites are not replacement for [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and school. Don't use such sites as any kind of learning or teaching resource, because they aren't. – Some programmer dude Aug 14 '22 at 11:14
  • 1
    You also read `element` which is uninitialized when you call `IN`. This makes your program have undefined behavior. You also do `Array[i].count++` for every `i` in the loop in `count_Occurence`. See the logic flaw? – Ted Lyngmo Aug 14 '22 at 11:26

1 Answers1

2

I initialized the value of count in structure definition as 0 but it isn't getting initialized as zero when I ran it. It's some garbage value. I am not able to understand why?

Well, that is what happens when you use C tools in C++ without proper undestanding about what they do and don't, in this case they ignore constructors.

malloc does not create any Occurence objects, casting is UB.

Just use std::vector, std::unique_ptr, or at least new Occurence[N].

I strongly suggest you switch to modern c++ instead of this half-C half-incorrect-C++ setup that many online puzzle-based websites "teach".

Quimby
  • 17,735
  • 4
  • 35
  • 55