Given some integers, return the k most frequent elements Given a non-empty array of integers, return the k most frequent elements.

For example, given [1,1,1,2,2,3] and k = 2, return [1,2].

Following is my submission to this problem on Leetcode and it is accepted. Can you please provide feedback on any issue that you see in this code? I am looking for improvements in the quality of the code from both software engineering perspective as well as from algorithmic complexity or space and run-time performance perspective.

#include &lt;algorithm&gt;
#include &lt;map&gt;
#include &lt;vector&gt;

using namespace std;


class Node
{
public:

    int value{};
    unsigned frequency{};

    Node()
    {

    }

    Node (int value, unsigned frequency)
    {
        this-&gt;value = value;
        this-&gt;frequency = frequency;
    }

    bool operator&lt; (const Node&amp; other)
    {
        return this-&gt;frequency &lt; other.frequency;
    }
};

class Solution {
public:

    vector&lt;int&gt; topKFrequent(vector&lt;int&gt;&amp; nums, int k) 
    {            
        map&lt;int, unsigned&gt; frequency;

        for (auto n : nums )
        {
            if ( frequency.find(n) != frequency.end())
                frequency[n]++;
            else
                frequency[n] = 0;
        }

        vector&lt;Node&gt; heap;

        for ( auto f : frequency )
            heap.push_back(Node(f.first, f.second));

        make_heap(heap.begin(), heap.end());

        vector&lt;int&gt; result;

        unsigned i = 0;
        while ( i &lt; k )
        {  
            result.push_back(heap.front().value);
            pop_heap(heap.begin(), heap.end()-i);
            i++;
        }


        return result;
    }
};
Just the general C++ coding style treatment.


Do not use using namespace std;. It can introduce subtle bugs, makes the code harder to read and writing out the std:: prefix generally is not going to be to time intensive.
Keep your spacing consistent. Compare the following two lines

bool operator&lt; (const Node&amp; other)
vector&lt;int&gt; topKFrequent(vector&lt;int&gt;&amp; nums, int k)


Why is one method name followed by a space while the other is not? Consistency is important! If you are not sure which spacing variant to choose, you should read a C++ style guide. The same applies to control structures, such as 

for (auto n : nums )
if ( frequency.find(n) != frequency.end())
for ( auto f : frequency )

Use a member initializer list in your constructor. Although it does not matter in a lot of cases, it is a more concise way of expressing member initialization and leaves the constructor's body open for more complex setup tasks. The Node constructor would then look something like this:

Node(int value, unsigned frequency) : value(value), frequency(frequency) { }


Also, since you default constructor does not actually do anything, you should remove it and the initializers for value and frequency and add default values to the constructor parameters instead, so that Node's constructor becomes something like

Node(int value = 0, unsigned frequency = 0) : value(value), frequency(frequency) { }


Keep in mind, however, that this constructor allows implicit conversions from integer types, which you typically want to avoid. This is easily prevented by making the constructor explicit.
Most people prefer to not write this-&gt; explicitly when not needed.
const-correctness is important. You should take the parameter nums in topKFrequent by const-reference since you do not actually modify it.
Always use curly brackets with if,else,for,while etc. This prevents a lot of errors from statements not being included in conditionally dependent blocks when they should be. In fact, this kind of error is so common that gcc even introduced a diagnostic for misleading indentation in version 6.