Two Lines of C++ Code to Convert an Array into Sorted Unique Elements by justyy

View this thread on steempeak.com
· @justyy ·
$12.08
Two Lines of C++ Code to Convert an Array into Sorted Unique Elements
Let's say, we want to extract the unique elements (numbers) in a given array (vector, list) and return them in sorted order. For example,

The array is [1, 1, 0, 0, 2], and the output is [0, 1, 2].

You can write a method in a straightforward method with two steps - extract unique numbers and then <a href="https://helloacm.com/how-to-sort-a-linked-list-by-converting-to-array-vector/" title="How to Sort a Linked List by Converting to Array/Vector?">sort</a> them.

<pre lang=c>
template &lt;class T&gt;
vector&lt;T&gt; uniqueAndSorted(const vector&lt;T&gt; &arr) {
    unordered_set&lt;T&gt; data;
    vector&lt;T&gt; ans;
    for (const auto &n: arr) {
        if (data.count(n)) continue;
        data.insert(n);
        ans.push_back(n);
    }
    sort(begin(ans), end(ans));
    return ans;
}
</pre>

You can, implement this with the following two liner - which makes use of the set (which maintains the key in sorted order). First, we construct the set based on the given array - which will give us a set that contains <a href="https://helloacm.com/the-unique-permutations-algorithm-with-duplicate-elements/" title="The Unique Permutations Algorithm with Duplicate Elements">unique</a> elements. Then we just need to construct the returned vector/array and the items will be automatically in sorted order as the set maintains the keys in ascending order.

<pre lang=c>
template &lt;class T&gt;
vector&lt;T&gt; uniqueAndSorted(const vector&lt;T&gt; &arr) {
    set&lt;int&gt; ans(begin(arr), end(arr));
    return vector&lt;T&gt;(begin(ans), end(ans));
}
</pre>

--EOF (<a title='The Ultimate Computing & Technology Blog' href='https://helloacm.com'>The Ultimate Computing & Technology Blog</a>) --

Reposted to [Blog of Computing](https://helloacm.com/two-lines-of-c-code-to-convert-an-array-into-sorted-unique-elements/)

---------------

Follow me for topics of *Algorithms, Blockchain and Cloud.*
I am @justyy - a Steem Witness
[https://steemyy.com](https://steemyy.com)


**Steem On!~**
Every little helps! I hope this helps! 
------------------
- *[Computing & Technology](https://helloacm.com/)*
- *[Download Youtube Video](https://weibomiaopai.com/download-video-parser.php)*
- Find Cheap & Bargin VPS: *[VPS Database](https://anothervps.com/vps-database/)*
- [Online Software and Tools](https://steakovercooked.com/Software.Home)

If you like my work, please consider voting for me or [Buy Me a Coffee](https://justyy.com/out/buymeacoffee), thanks!
https://steemit.com/~witnesses type in **justyy** and click ***VOTE***
https://steemyy.com/images/vote-for-justyy.jpg
<BR/>
**Alternatively, you could [proxy to me](https://steemyy.com/witness-voting/?witness=justyy&action=proxy)  if you are too lazy to vote!**

Also: you can vote me at the tool I made:  https://steemyy.com/witness-voting/?witness=justyy
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 55 others
properties (23)
post_id87,830,837
authorjustyy
permlinktwo-lines-of-c-code-to-convert-an-array-into-sorted-unique-elements
categoryprogramming
json_metadata{"tags":["programming","wherein","wherein-daka","blog","codeonsteem","cplusplus","coding","tech"],"users":["justyy"],"image":["https:\/\/steemyy.com\/images\/vote-for-justyy.jpg"],"links":["https:\/\/helloacm.com\/how-to-sort-a-linked-list-by-converting-to-array-vector\/","https:\/\/helloacm.com\/the-unique-permutations-algorithm-with-duplicate-elements\/","https:\/\/helloacm.com","https:\/\/helloacm.com\/two-lines-of-c-code-to-convert-an-array-into-sorted-unique-elements\/","https:\/\/steemyy.com","https:\/\/helloacm.com\/","https:\/\/weibomiaopai.com\/download-video-parser.php","https:\/\/anothervps.com\/vps-database\/","https:\/\/steakovercooked.com\/Software.Home","https:\/\/justyy.com\/out\/buymeacoffee","https:\/\/steemit.com\/~witnesses","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&action=proxy","https:\/\/steemyy.com\/witness-voting\/?witness=justyy"],"app":"steemit\/0.2","format":"markdown"}
created2020-10-04 12:47:15
last_update2020-10-04 12:47:15
depth0
children0
net_rshares34,303,469,256,585
last_payout2020-10-11 12:47:15
cashout_time1969-12-31 23:59:59
total_payout_value6.218 SBD
curator_payout_value5.857 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2,821
author_reputation2,010,635,440,258,493
root_title"Two Lines of C++ Code to Convert an Array into Sorted Unique Elements"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (119)