0
 bool containsDuplicate(vector<int>& nums) {
    int n=nums.size();
    int* counter=new int[n];
    for(int i=0;i<n-1;++i){
        ++counter[i];
    }
    for(int i=0;i<n;++i){
        if(int i>1) return true;
    }
    return false;
}

As you can see,I applied for a heap space'counter'; So,I want know if I need to free the heap memory,how to free the heap memory? Thank you.

trincot
  • 317,000
  • 35
  • 244
  • 286
jg c
  • 27
  • 3
  • Does this answer your question? [deleting c++ array from heap and memory leak](https://stackoverflow.com/questions/47111302/deleting-c-array-from-heap-and-memory-leak) – Abdur Rakib Jan 14 '22 at 12:47
  • 6
    `if(int i>1)` that does not look like valid C++. If your compiler ate this without complaints, throw it out and get a better one. –  Jan 14 '22 at 12:50
  • 1
    `++counter[i];` is not going to behave as `counter` has not been initialised. Please red [ask] with a [mcve]; the code should compile unless you are asking about compilation errors. Why mix `std::vector` and c-arrays? and yes you need to `delete [] counter;` – Richard Critten Jan 14 '22 at 12:57
  • 2
    why not use a `std::vector counter;` ? – 463035818_is_not_an_ai Jan 14 '22 at 13:10
  • 2
    You are already using `vector` in your program, but you didn't use it where it could have been used: `std::vector counter (n);`. Then all the issues are solved. – PaulMcKenzie Jan 14 '22 at 13:39
  • If I'm not mistaken thie function will always return `false` if you initialize `counter` to contain 0's. For some reason it also ignore the last element of `counter`. – Lukas-T Jan 14 '22 at 13:39
  • 1
    Also, this code would fail to find a duplicate for something simple as `{1, 10 ,1}`, even if you fixed all the issues, unless you are assuming that `nums` contains only integers from `0` to `n-1`. – PaulMcKenzie Jan 14 '22 at 13:55
  • @PaulMcKenzie Well, the function would also fail for `{0, 1, 1}`, because it doesn't actually read `nums`, except in `int n=nums.size();`. – Lukas-T Jan 14 '22 at 15:13

1 Answers1

1

First the direct answers to your questions, but read on after that:

I want know if I need to free the heap memory

Yes, you should.

how to free the heap memory?

delete [] counter;

Note that the [] is essential as counter is a dynamic array. For non-array memory allocated with new, the [] should be omitted (see cppreference.com).

Issues in your code

  1. if(int i>1) is not valid C++ syntax. It should just be if(i>1)

  2. counter is not initialised, so the outcome is not reliable.

  3. The first loop does not count occurrences of values but of indices, so (when the previous issues are fixed) the returned value will always be false.

  4. If all of the above is fixed, this function can only deal with values that are in the range {0, n-1}, as other values are not valid indices of the counter array. Use a set instead. This also makes the question on memory management irrelevant, as the set will be destroyed when the function exits.

Proposed solution

Once the decision is taken to use a set, things become a lot easier. The set constructor can be given a range (of the nums vector) and once that set is contructed, it suffices to compare its size with that of the vector:

#include <vector>
#include <set>
using namespace std;

bool containsDuplicate(vector<int>& nums) {
    set<int> uniques(nums.begin(), nums.end());
    return uniques.size() < nums.size();
}
trincot
  • 317,000
  • 35
  • 244
  • 286