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 <class T> vector<T> uniqueAndSorted(const vector<T> &arr) { unordered_set<T> data; vector<T> 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 <class T> vector<T> uniqueAndSorted(const vector<T> &arr) { set<int> ans(begin(arr), end(arr)); return vector<T>(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
post_id | 87,830,837 |
---|---|
author | justyy |
permlink | two-lines-of-c-code-to-convert-an-array-into-sorted-unique-elements |
category | programming |
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"} |
created | 2020-10-04 12:47:15 |
last_update | 2020-10-04 12:47:15 |
depth | 0 |
children | 0 |
net_rshares | 34,303,469,256,585 |
last_payout | 2020-10-11 12:47:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 6.218 SBD |
curator_payout_value | 5.857 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 2,821 |
author_reputation | 2,010,635,440,258,493 |
root_title | "Two Lines of C++ Code to Convert an Array into Sorted Unique Elements" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gerber | 0 | 5,715,061,181 | 4.2% | ||
ezzy | 0 | 224,770,959,191 | 4.2% | ||
exyle | 0 | 334,196,472,998 | 4.2% | ||
ace108 | 0 | 225,222,007,399 | 18% | ||
remlaps | 0 | 20,616,289,651 | 25% | ||
shanghaipreneur | 0 | 491,005,470 | 100% | ||
justyy | 0 | 26,402,513,776,177 | 100% | ||
happyukgo | 0 | 16,602,307,454 | 100% | ||
elizacheng | 0 | 11,063,709,575 | 5.25% | ||
fleur | 0 | 12,750,568,557 | 100% | ||
ew-and-patterns | 0 | 22,931,785,641 | 4.2% | ||
apoloo1 | 0 | 14,418,022,475 | 6.79% | ||
robinlee | 0 | 487,510,956 | 100% | ||
dine77 | 0 | 147,678,922,241 | 6.53% | ||
ety001 | 0 | 17,808,477,189 | 50% | ||
rainyapril | 0 | 472,704,723 | 100% | ||
khalil319 | 0 | 305,635,251 | 10% | ||
oldman28 | 0 | 21,402,763,541 | 60% | ||
ahlawat | 0 | 11,944,222,209 | 25% | ||
kikima | 0 | 774,643,856 | 100% | ||
geass | 0 | 649,374,615 | 100% | ||
tvb | 0 | 76,557,946,243 | 99.79% | ||
karja | 0 | 63,197,985,288 | 3% | ||
floatinglin | 0 | 4,118,561,416 | 100% | ||
accelerator | 0 | 23,213,358,974 | 4.2% | ||
rosatravels | 0 | 631,949,490,774 | 100% | ||
superbing | 0 | 4,447,275,031 | 100% | ||
dailyfortune | 0 | 3,535,760,377 | 100% | ||
dobartim | 0 | 584,914,294,952 | 42% | ||
dailystats | 0 | 5,673,542,175 | 100% | ||
steemflower | 0 | 154,730,200,325 | 70% | ||
khaleelkazi | 0 | 356,506,132 | 4.2% | ||
jianan | 0 | 29,689,560,937 | 100% | ||
cnbuddy | 0 | 1,705,320,119 | 0.1% | ||
anxin | 0 | 2,784,687,187 | 100% | ||
joslud | 0 | 2,959,122,767 | 100% | ||
mmmmkkkk311 | 0 | 861,967,660,710 | 100% | ||
nealmcspadden | 0 | 2,503,280,149 | 4.2% | ||
mermaidvampire | 0 | 34,974,069,185 | 100% | ||
kimjaguar | 0 | 981,073,390 | 12.5% | ||
nean | 0 | 490,825,809 | 100% | ||
foodielifestyle | 0 | 449,286,071 | 100% | ||
woolfe19861008 | 0 | 2,350,367,474 | 100% | ||
dailychina | 0 | 6,863,796,061 | 100% | ||
vincenthan | 0 | 529,445,334 | 100% | ||
dongfengman | 0 | 12,485,237,692 | 100% | ||
serenazz | 0 | 1,222,858,007 | 100% | ||
yedda | 0 | 488,026,680 | 100% | ||
zasilla | 0 | 474,569,295 | 100% | ||
ethanlee | 0 | 5,319,032,698 | 100% | ||
twinsnicole | 0 | 477,764,080 | 100% | ||
deepthinking | 0 | 496,120,103 | 100% | ||
fanso | 0 | 1,579,678,450 | 100% | ||
barmbo | 0 | 75,106,695,003 | 100% | ||
yameen | 0 | 8,547,290,224 | 30% | ||
lilypang22 | 0 | 1,400,386,016 | 100% | ||
zens | 0 | 488,613,239 | 100% | ||
steemitvip | 0 | 474,538,527 | 100% | ||
regals | 0 | 483,109,882 | 100% | ||
joelone | 0 | 488,314,033 | 100% | ||
kirstin | 0 | 145,986,147,256 | 100% | ||
sweet-jenny8 | 0 | 24,293,251,194 | 100% | ||
myskye | 0 | 12,046,113,999 | 100% | ||
chineselaw | 0 | 146,870,675,364 | 100% | ||
steemeditor.bot | 0 | 1,268,401,076 | 50% | ||
weicomenew | 0 | 485,065,418 | 100% | ||
laiyuehta | 0 | 4,958,051,142 | 100% | ||
turtlegraphics | 0 | 2,691,363,752 | 100% | ||
glory7 | 0 | 57,500,177,332 | 12.5% | ||
jayplayco | 0 | 13,830,146,795 | 25% | ||
dlike | 0 | 81,631,108,197 | 6% | ||
witnesstools | 0 | 7,734,537,542 | 100% | ||
upyougo | 0 | 225,862,024 | 100% | ||
ilovecoding | 0 | 7,205,490,485 | 100% | ||
moneytron | 0 | 4,082,199,928 | 100% | ||
teriyaki | 0 | 0 | 100% | ||
sapwood | 0 | 227,041,912,490 | 100% | ||
sojucaps | 0 | 525,841,211 | 100% | ||
steemfuckeos | 0 | 3,502,086,905 | 100% | ||
edgarare1 | 0 | 359,181,569,662 | 100% | ||
kakakk | 0 | 552,598,915 | 3% | ||
ctime | 0 | 825,006,008,668 | 100% | ||
skymin | 0 | 33,583,146,587 | 50% | ||
cpt-sparrow | 0 | 23,267,268,820 | 100% | ||
yogaworld | 0 | 167,987,349,005 | 100% | ||
sofialanz | 0 | 188,760,711 | 6.79% | ||
plankton.token | 0 | 44,776,309,749 | 75% | ||
quantumdeveloper | 0 | 5,018,629,321 | 100% | ||
blockcleaner | 0 | 26,497,611,194 | 100% | ||
steem.leo | 0 | 4,904,739,126 | 4.2% | ||
cplusplus | 0 | 7,026,607,033 | 100% | ||
leo.voter | 0 | 253,759,437 | 4.2% | ||
justsoso | 0 | 1,828,276,337 | 100% | ||
perrymine | 0 | 88,615,221 | 10% | ||
dcinside | 0 | 397,998,782 | 12.5% | ||
maxuvd | 0 | 4,596,052,547 | 11% | ||
maxuve | 0 | 9,695,975,553 | 4.03% | ||
longhash | 0 | 1,568,262,408 | 100% | ||
llfestyle | 0 | 353,297,960 | 100% | ||
javb | 0 | 5,480,521,856 | 6.79% | ||
levi-miron | 0 | 80,230,007,173 | 100% | ||
photosnap | 0 | 1,686,262,691 | 20% | ||
sexybeach | 0 | 7,119,580,642 | 100% | ||
suckmydick | 0 | 3,486,426,658 | 100% | ||
chromebook | 0 | 3,509,746,217 | 100% | ||
successgr | 0 | 257,820,114,023 | 3% | ||
ericandryan | 0 | 22,346,975,946 | 100% | ||
kjinhyun | 0 | 126,307,570,518 | 55% | ||
jayplay.witness | 0 | 257,770,174 | 12.5% | ||
beargame | 0 | 300,461,400,378 | 25% | ||
justyy222 | 0 | 3,411,498,318 | 100% | ||
juddsmith079 | 0 | 228,628,383,131 | 25% | ||
roundblocknew | 0 | 346,207,138,225 | 25% | ||
bugabuga | 0 | 8,898,840,702 | 5% | ||
mildaleblanc | 0 | 249,827,040 | 25% | ||
pharos1999 | 0 | 275,967,968 | 25% | ||
pluepark | 0 | 2,241,221,248 | 25% | ||
patorney | 0 | 533,693,976,388 | 10% | ||
projectw | 0 | 9,212,886,984 | 25% |