What's the most efficient way to print all elements of vector in ascending order till it's empty without duplicates?

what'd
what w
whatsapp app
what c
what's
what p
what h
whatsapp login

I'm supposed to:

  1. Print vector elements sorted without repetition.
  2. Delete the elements that are printed from vector.
  3. Repeat the the previous steps until vector is empty.

But it seems that my code takes more time so, I seek for optimisation. I've tried to do this task with std::vector and std::set. Here is my approach:

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

int main () {
    int n;
    cin >> n;
    vector<int> v(n);
    set<int> st;

    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }

    while (!v.empty()) {
        for (int i = 0; i < v.size(); i++)
            st.insert(v[i]);
        for (auto x : st) {
            cout << x << ' ';
            auto it = find(v.begin(), v.end(), x);
            if (it != v.end())
                v.erase(it);
        }
        st.clear();
        cout << "\n";
    }

    return 0;
}

For example input is like:

7
1 2 3 3 2 4 3

Output gonna be like this:

1 2 3 4 
2 3 
3

You might use std::map instead of std::vector/std::set to keep track of numbers:

#include <iostream>
#include <map>

int main () {
    map<int, int> m;

    int size;
    std::cin >> size;

    for (int i = 0; i != size; i++) {
        int number;
        std::cin >> number;
        ++m[number];
    }

    while (!m.empty()) {
        for (auto it = m.begin(); it != m.end(); /*Empty*/) {
            const auto number = it->first;
            auto& count = it->second;
            std::cout << number << ' ';
            if (--count == 0) {
                it = m.erase(it);
            } else {
                ++it;
            }
        }
        std::cout << "\n";
    }
}

Complexity is now O(n log(n)) instead of O(n²) (with lot of internal allocations).

What, Quickly send and receive WhatsApp messages right from your computer. Quickly send and receive WhatsApp messages right from your computer.

Due to it overwriting the elements expected to be deleted, std::unique won't be much use for this problem. My solution:

std::sort(v.begin(), v.end());

while (!v.empty())
{
    int last = v.front();
    std::cout << last << " ";
    v.erase(v.begin());

    for (auto it = v.begin(); it != v.end(); /* no-op */)
    {
        if (*it == last)
        {
            ++it;
        }
        else
        {
            last = *it;
            std::cout << last << " ";
            it = v.erase(it);
        }
    }

    std::cout << std::endl;
}

You could probably improve performance further by reversing the sorting of the vector, and then iterating through backwards (since it's cheaper to delete from closer to the back of the vector), but that would complicate the code further, so I'll say "left as an exercise for the reader".

Drake - Nice For What, I will do what I can to help you. TranslationsEdit. show ▽what? (interrogative pronoun)  noun the true nature or identity of something, or the sum of its characteristics: a lecture on the whats and hows of crop rotation.

You can use std::map

auto n = 0;
std::cin >> n;

std::map<int, int> mp;
while (--n >= 0) {
    auto i = 0;
    std::cin >> i;
    mp[i] += 1;
}

while (!mp.empty()) {
    for (auto& it: mp) {
        std::cout << it.first << " ";
        it.second--;
    }

    for (auto it = mp.begin(); it != mp.end(); ++it) {
        if (it->second == 0) mp.erase(it);
    }
    std::cout << "\n";
}

without any erase

auto n = 0;
std::cin >> n;

std::map<int, int> mp;
while (--n >= 0) {
    auto i = 0;
    std::cin >> i;
    mp[i] += 1;
}

auto isDone = false;
while (!isDone) {
    isDone = true;
    for (auto& it: mp) {
        if (it.second > 0) std::cout << it.first << " ";
        if (--it.second > 0) isDone = false;
    }

    std::cout << "\n";
}

WhatsApp Web, 20 million people lost their jobs in one month. What did you expect?” If we quickly develop a vaccine and optimism returns people will say, “Of  informal a punishment or reprimand (esp in the phrase give (a person) what for) 7. what have you someone, something, or somewhere unknown or unspecified: cars, motorcycles, or what have you. 8.

Here is a solution using sort and vector. It uses a second vector to hold the unique items and print them.

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::vector<int> v{1,2,3,3,2,4,3};
    std::sort(v.begin(), v.end());
    std::vector<int>::iterator vit;
    while(!v.empty()){
        std::vector<int> printer;
        std::vector<int>::iterator pit;
        vit = v.begin();
        while (vit != v.end()){
            pit = find(printer.begin(), printer.end(), *vit);
            if (pit == printer.end()){
                printer.push_back(*vit);
                vit = v.erase(vit);
            } else {
                ++vit;
            }
        }
        std::copy(printer.begin(), printer.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << '\n';
    }
}

Output:

1 2 3 4
2 3
3

what, What Experts Are Learning About Symptoms : Goats and Soda It's not just a fever and dry cough. For milder cases of COVID-19, the array of  From Middle English what, from Old English hwæt (“ what ”), from Proto-Germanic *hwat (“ what ”), from Proto-Indo-European *kʷód (“ what ”), neuter form of *kʷós (“ who ”).

It's not clear (at least to me) exactly what you're talking about when you mention "efficiency". Some people use it to refer solely to computational complexity. Others think primarily in terms of programmer's time, while still others think of overall execution speed, regardless of whether that's obtained via changes in computational complexity, or (for one example) improved locality of reference leading to better cache utilization.

So, with that warning, I'm not sure whether this really improves what you care about or not, but it's how I think I'd do the job anyway:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

// preconditions: input range is sorted
template <class BidiIt>
BidiIt partition_unique(BidiIt begin, BidiIt end) {
    auto pivot = end;
    for (auto pos = begin; pos != pivot; ++pos) {
        auto mid = std::next(pos);

        for ( ; mid < pivot && *mid == *pos; ++mid, --pivot)
            ;
        std::rotate(std::next(pos), mid, end);
    }
    return pivot;
}

template <class It>
void show(It b, It e, std::ostream &os) {
    while (b != e) {
        os << *b << ' ';
        ++b;
    }
    os << '\n';
}

int main() {
    std::vector<int> input{ 1, 2, 3, 3, 2, 4, 3 };

    std::sort(input.begin(), input.end());

    auto begin = input.begin();

    auto pos = begin;
    while ((pos = partition_unique(begin, input.end())) != input.end()) {
        show(begin, pos, std::cout);
        begin = pos;
    }
    show(begin, input.end(), std::cout);
}

I'm not really sure it's possible to improve the computational complexity much over what this does (but it might be--I haven't thought about it enough to be sure one way or the other). Compared to some versions I see posted already, there's a decent chance this will improve overall speed (e.g., since it just moves things around inside the same vector, it's likely to get better locality than those that copy data from one vector to another.

What Have We Learned Here? · Collaborative Fund, As countries lie frozen in lockdown and billions of people lose their livelihoods, public figures are teasing a breakthrough that would mark the  WhatsApp Messenger: More than 2 billion people in over 180 countries use WhatsApp to stay in touch with friends and family, anytime and anywhere. WhatsApp is free and offers simple, secure, reliable messaging and calling, available on phones all over the world.

Is It COVID-19 Or Something Else? What Experts Are Learning , Now What? After-school programs are closed. Day care centers are shut down. Summer camp might not happen. If you're stuck with money you  Release year: 2019. Desperate to secure funding for her med tech startup, an idealistic scientist and her husband strike an outrageous deal with a mysterious investor. Putting their trust on the line, a couple considers a proposal from a notorious investor: a night with Sean in exchange for funding Lisa’s startup.

What happens if a coronavirus vaccine is never developed?, Some, she says, are “full cancellations,” while “other people are just putting searches on pause to see what happens in the next couple of  Find out what your public IPv4 and IPv6 address is revealing about you! My IP address information shows your location; city, region, country, ISP and location on a map.

There's Money Stuck in Your Dependent Care Account. Now What , Seen a font in use and want to know what it is? Upload an image to WhatTheFont to find the closest matches in our database.

Comments
  • Why not the output is 1 2 3 4 ; 2 3 4; 3 4; 4 ?? why 4 is ignored??
  • @Praveen Because we remove 1 2 3 4 from the vector so the remaining element would be only 2 3 3 and so on.
  • std::unordered_map tends to be much faster than std::map, even with an additional std::copy to a vector and std::sort at the end. Benchmarking suggested. (And then there are third-party hash tables that are much faster still.)
  • @Jarod42 there's a missing semicolon int number and use it != m.end(); instead of m != m.end();
  • @MohamedMagdy: typos fixed.
  • Even with reverse_iterator, complexity is O(n²).
  • @Jarod42 true; your answer is more efficient. And since a sort is required at minimum, it won't be beaten on time complexity.
  • But this just printed 1 2 3 4. 2 3 and 3 in the next two lines are missed.
  • 1 2 3 4 ; 2 3 4; 3 4; 4 why not 4... I'll update the answer to get the output..
  • You misunderstood the question.
  • You don't improve time complexity of the program.