[Programming Problem] Subsets II

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:

Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]

Example 2:

Input: nums = [0]
Output: [[],[0]]

[Problem Link]

Let’s use backtracking to create our subsets. We’ll use a Set to know if the subset if already in our return array (key is simply concatenated version of the array).

[1, 2, 1]

Index 3: []
Index 2: [] [1]
Index 1: [][1] [2][2,1]
Index 0: [][1][2][2,1] [1][1,1][1,2][1,2,1]

Notice what happened here. The same subset [1, 2] and [2, 1] get counted as different subsets. To avoid this we run the same algorithm on the sorted version of the input.

[1, 1, 2]

Index 3: []
Index 2: [] [2]
Index 1: [][2] [1][1,2]
Index 0: [][2][1][1,2] [1][1,2][1,1][1,1,2]

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsetsWithDup = function(nums) {
    return subsetsWithDupCalc(0, nums.sort(), new Set());
};
 
var subsetsWithDupCalc = function(i, nums, retSet) {
    if (i === nums.length) return [[]];
 
    let ret = subsetsWithDupCalc(i+1, nums, retSet);
    let newRet = [...ret];
    ret.forEach((arr) => {
        let curr = [nums[i], ...arr];
        if (!retSet.has(curr.join(''))) {
            newRet.push(curr);
            retSet.add(curr.join(''));
        }
    });
 
    return newRet;
};

87 thoughts on “[Programming Problem] Subsets II

  1. I precisely desired to thank you so much again. I do not know the things I would have done without those techniques provided by you directly on this area. It was actually an absolute hard dilemma in my opinion, however , spending time with your expert strategy you dealt with it forced me to leap for fulfillment. I’m thankful for this support as well as trust you really know what a great job you are always getting into training some other people thru a web site. I know that you’ve never got to know any of us.

  2. I’m just commenting to let you be aware of of the impressive encounter my cousin’s princess went through viewing the blog. She came to find plenty of things, most notably what it’s like to have an amazing teaching spirit to let certain people with no trouble know some very confusing topics. You really exceeded my expectations. I appreciate you for showing such necessary, safe, edifying and as well as fun tips about that topic to Lizeth.

  3. I wanted to compose you this very small word to thank you again with your great strategies you’ve discussed above. It’s really seriously open-handed with you to present extensively precisely what some people would’ve distributed for an ebook to make some cash for their own end, especially since you might have done it if you ever desired. These pointers in addition served like the easy way to recognize that the rest have the same keenness the same as my personal own to know good deal more on the topic of this matter. I am certain there are numerous more fun occasions ahead for people who read carefully your site.

  4. I and also my guys appeared to be viewing the best guides from your site and so the sudden I had an awful feeling I had not thanked the blog owner for them. All the ladies appeared to be so glad to read all of them and already have honestly been taking pleasure in them. Many thanks for indeed being indeed kind and then for using these kinds of useful subject matter millions of individuals are really desirous to be aware of. My personal sincere apologies for not expressing appreciation to you earlier.

  5. I have to point out my love for your kind-heartedness supporting men who should have assistance with this particular study. Your real dedication to passing the message all over came to be pretty powerful and has without exception empowered workers much like me to realize their targets. This informative facts denotes so much to me and further more to my office colleagues. Regards; from everyone of us.

  6. I want to get across my affection for your generosity in support of men who require help with your concern. Your very own dedication to passing the solution along has been pretty significant and has specifically empowered somebody like me to reach their ambitions. Your personal informative instruction denotes so much to me and even further to my office colleagues. Thank you; from each one of us.

  7. I wanted to put you that bit of remark to be able to thank you very much yet again about the striking tactics you’ve featured on this page. It is incredibly generous with people like you to allow unreservedly all that many of us could possibly have supplied as an e book in making some dough for themselves, and in particular given that you might well have tried it in the event you wanted. These basics in addition worked to be the great way to be aware that someone else have the identical zeal really like my personal own to understand whole lot more when considering this matter. I think there are many more pleasant situations ahead for those who find out your website.

  8. I simply wished to thank you so much yet again. I am not sure the things I might have created in the absence of the entire aspects shown by you directly on my field. It was a traumatic condition in my view, however , taking a look at the professional fashion you dealt with it made me to weep for happiness. Now i’m happier for the information as well as trust you are aware of an amazing job you were accomplishing educating people all through a web site. Most probably you’ve never got to know all of us.

  9. Needed to write you that bit of remark to give many thanks yet again for those superb basics you have contributed here. It is simply pretty open-handed of you to give easily what many of us could possibly have advertised for an e-book to help make some money for their own end, precisely considering that you might well have done it if you wanted. Those ideas in addition served to provide a easy way to realize that some people have a similar interest just as my own to realize a little more on the subject of this condition. I believe there are some more pleasurable times up front for people who look over your website.

  10. Thanks a lot for giving everyone an extraordinarily wonderful opportunity to read from here. It is often so awesome and packed with a great time for me personally and my office co-workers to visit your site not less than three times every week to find out the latest items you will have. And of course, I’m also actually contented with your striking creative concepts you serve. Selected two ideas in this post are ultimately the most suitable we’ve had.

  11. I want to point out my affection for your kind-heartedness for people that actually need assistance with in this niche. Your special dedication to getting the solution across appeared to be surprisingly significant and have truly allowed many people like me to achieve their dreams. Your entire useful useful information means a great deal a person like me and substantially more to my office colleagues. Best wishes; from everyone of us.

  12. Thank you a lot for providing individuals with an extremely marvellous opportunity to discover important secrets from here. It’s usually so great plus stuffed with a great time for me and my office colleagues to search your site more than thrice every week to see the new stuff you have. Of course, I’m at all times fascinated considering the powerful ideas you give. Selected 4 tips in this article are unequivocally the simplest we’ve ever had.

  13. I must point out my admiration for your generosity for persons who must have guidance on this particular issue. Your special commitment to passing the solution around has been wonderfully beneficial and have continually empowered some individuals like me to realize their goals. Your new interesting suggestions means a whole lot to me and even further to my office workers. Warm regards; from everyone of us.

  14. I precisely wished to thank you very much once more. I’m not certain the things that I would’ve worked on in the absence of the entire secrets provided by you regarding this subject matter. It absolutely was a very frightful circumstance in my view, nevertheless taking note of a skilled manner you dealt with that made me to weep with happiness. Now i am grateful for the service and in addition trust you realize what an amazing job you happen to be providing instructing people today through the use of your site. Probably you’ve never got to know all of us.

  15. Needed to post you the little word to be able to give many thanks the moment again regarding the fantastic solutions you’ve discussed on this site. It’s simply seriously generous of people like you to present publicly exactly what many people could possibly have distributed as an e book to make some money for their own end, especially given that you could possibly have tried it if you ever decided. These concepts additionally worked as the good way to fully grasp that some people have the same dreams just like my very own to figure out more and more around this condition. I’m certain there are lots of more fun situations up front for many who read through your site.

  16. Thanks a lot for giving everyone a very terrific possiblity to read critical reviews from here. It really is very amazing plus full of a great time for me and my office acquaintances to search the blog nearly 3 times weekly to see the newest items you have got. Of course, I’m just usually happy with the fabulous hints you give. Some 2 areas in this article are undoubtedly the simplest I’ve had.

  17. I would like to point out my gratitude for your kind-heartedness giving support to those who really want help on in this matter. Your very own commitment to getting the solution around came to be quite interesting and have usually helped most people just like me to attain their dreams. This invaluable guide can mean a whole lot a person like me and still more to my office colleagues. Best wishes; from everyone of us.

  18. I have to voice my admiration for your kind-heartedness in support of folks that really need guidance on this area of interest. Your very own dedication to passing the solution around was extremely interesting and has usually permitted guys and women like me to achieve their pursuits. Your amazing warm and friendly facts indicates much to me and far more to my colleagues. Regards; from all of us.

  19. I precisely needed to thank you very much once again. I am not sure the things I would have sorted out without the type of suggestions shown by you regarding such subject. It seemed to be a real fearsome scenario in my circumstances, however , witnessing your specialized tactic you resolved that took me to cry for happiness. I’m thankful for this advice and then hope that you recognize what a powerful job that you are undertaking instructing the mediocre ones using a web site. I’m certain you’ve never encountered all of us.

  20. My spouse and i ended up being so fulfilled when Michael managed to conclude his research through the ideas he obtained through the web site. It’s not at all simplistic to simply choose to be offering helpful tips that many some people may have been selling. And we also realize we have got the writer to be grateful to because of that. The entire illustrations you have made, the straightforward blog menu, the friendships you will assist to foster – it’s everything incredible, and it’s really assisting our son in addition to the family recognize that that subject matter is interesting, and that’s really fundamental. Thank you for everything!

  21. }{Always remember that there are tons of different options available to you when looking for hair accessories. The sky is really the limit here, and you can choose from different braids and headbands, bows and ribbons, clips and curlers, and much, much more. Your wardrobe should include several hair accessories. A simple ponytail holder goes great with an athletic suit. So before you go out, make sure you’re carrying a few accessories with you.

  22. pgslot gotham win สมัครสล็อตเว็บไซต์ตรง กับเว็บไซต์ สล็อตเว็บไซต์ตรง pg slot ท่านสามารถสแกนไอดีไลน์ได้เลย แล้วกดที่ลงทะเบียนเป็นสมาชิก แล้วกรอกข้อมูลที่เราขอให้ครบถ้วน

  23. สมัครสมาชิกกับเราที่คุณจะได้รับคือการบริการที่ดีที่สุด pg พบกับเกมสล็อตที่หลากหลาย เกมไม่ซ้ำ มากกว่า 1000 เกม เล่นได้ตลอด 24 ชั่วโมง ไม่มีเบื่อ

  24. แจกฟรีโบนัสสำหรับการสมัครสมาชิกใหม่ 10รับ100 ระบบที่มีความเสถียรสูง ออโต้ ทั้งระบบ มีความแม่นยำ ไม่สะดุด

  25. เว็บใหม่ ใหญ่ที่สุดในเอเชีย สล็อตวอเลท พบกับความบันเทิงที่ไม่เคยหยุดนิ่ง พร้อมกับโบนัสที่ไหลลื่นไม่เคยหยุดยั้ง

  26. สนุกกับเกมสล็อตใหม่ล่าสุดได้ทั้งวันตลอด 24 ชั่วโมง pgslot คัดสรรมามอบให้กับคุณได้เต็มอิ่มกับความสุขบนมือถือ ที่ปลอดภัย

  27. แนะนำบริการ กิจกรรมพิเศษ สำหรับสมาชิกใหม่ pgdragon พบกับโปรโมชั่นสุดพิเศษของเรา ที่เราได้มอบให้กับสมาชิกทุกท่าน

  28. เกมยอดฮิตในปัจจุบัน เป็นเกมสล็อตน้องใหม่ เกมสล็อตแนวตั้ง pgslot ถือเป็นผู้ให้บริการเกมสล็อต น้องใหม่ ที่มาแรงมากในตอนนี้

  29. เว็บสล็อตตรง PG เกมสล็อต มือถือ อันดับ 1 dubai 1688 สล็อต สมัครฟรี ระบบ ออโต้สะดวกรวดเร็ว

  30. สล็อต PG เกมสล็อตแตกง่าย แตกบ่อย ยักษ์888 เกมสล็อตแตกง่าย แตกบ่อยค่ายเกมที่นักเดิมพันไม่ควรพลาด

  31. บริการฝาก-ถอนด้วยระบบออโต้หรือระบบอัตโนมัติ สล็อต pg วอเลท มีเกมสล็อตมากกว่า 100 เกมให้เลือกเล่นอย่างจุใจ

  32. เว็บบนมือถือได้เลยคุณจะได้พบกับความตื่นตาตื่นใจอย่างไม่เคยเจอมาก่อน เฮงเฮง 888 สล็อต เราให้บริการเกม pgslot โดยตรง ไม่ผ่านเอเย่นต์

  33. รูปแบบเกมมีความน่าสนใจมากขึ้น ไม่ว่าจะเป็น ซื้อฟรีเกมส์ ซื้อโบนัส pg-slot auto แจกกำไรให้ผู้เล่นมากขึ้น อัตราการจ่ายที่สูง ทำให้เกมจากค่าย pg

  34. เกมสล็อต ไม่มีขั้นต่ำ 1 บาท ก็ฝากได้ sb888 สล็อต ฝาก-ถอน อัตโนมัติที่ ได้รับความนิยมอย่างต่อเนื่องพัฒนาระบบที่นำมาใช้กับสล็อต

  35. พีจีสล็อต เว็บตรง ไม่ผ่านตัวแทน ซูชิ555 สล็อต เว็บตรง ไม่ผ่านตัวแทนเปิดให้บริการแล้ววันนี้ เล่นได้ทุกระบบไม่มีสะดุด

  36. เว็บเถรียร โหลดเร็ว ไม่ล่มบ่อย สล็อตสายบุญ มีทีมแอดมินมืออาชีพที่ให้บริการตลอดทั้งวัน ติดต่อได้ตลอด 24 ชม.

  37. pgslot เว็บตรง สล็อตแตกหนัก pg
    สล็อต เว็บตรง 2023 โอกาสชนะสูงสุด

  38. PGSLOT เกมสล็อต pgslot
    ที่ได้รับความนิยมมากที่สุดในตอนนี้

  39. ตอบสนองความเบิกบานใจ ให้กับท่าน พวกเราเป็นเป็นไซต์ pg slot เว็บไซต์ใหม่

  40. PG SLOT 5G พีจีสล็อตเว็บตรง ฝาก-ถอนไม่มีขั้นต่ำ pg slotถอนได้เต็มไม่ติดเทิร์น เล่นสนุกถอนสบาย 2023

  41. เล่นสล็อตที่เว็บตรง pgzeed
    ไม่ผ่านเอเย่นต์ ไม่มีขั้นต่ำ ยังเปิดโอกาสให้คุณสามารถรับโปรโมชั่นและโบนัสต่างๆ

  42. มีโอกาสชิงรางวัลใหญ่จากเกมส์สล็อตสุดฮิตpgzeed888ไม่ผ่านเอเย่นต์ ไม่มีขั้นต่ำ

  43. ที่สุดของ เว็บสล็อต pg slot
    ความสนุกและความตื่นเต้นที่ไม่มีที่สิ้นสุดกับ pgslot เว็บตรง

  44. ได้เก็บเกมที่มีคุณภาพจากทั้งโลกเพื่อสมาชิกได้เลือกเล่น20 รับ 100และก็พวกเราจะมานะอัพเดทแล้วก็เพิ่มเกมใหม่ๆที่น่าดึงดูดตลอดมา

  45. ขมักเขม้นที่จะสร้างเสริมประสบการณ์ที่เยี่ยมที่สุดสำหรับสมาชิก25 รับ 100ทั้งหมดทั้งปวงรวมทั้งการให้บริการที่รวดเร็วทันใจ การฝากถอนกล้วยๆแล้วก็ฐานเกมที่นานาประการ. ด้วยการลงทะเบียนที่คุณจะได้รับความเสียดสีที่ไม่เคยมีมาก่อน

  46. สิ่งที่ทำให้สะดุดตาเป็นการมีเกมสล็อตจากผู้พัฒนามากpgslot42 รวมทั้งโปรโมชันรวมทั้งโบนัสที่นานัปการ เพื่อเพิ่มสิ่งจูงใจให้ผู้เล่นมีความสนุกสนานร่าเริงรวมทั้งได้โอกาสทำเงินมากขึ้น

  47. คุณไปพินิจพิเคราะห์รวมทั้งเลือกเว็บไซต์สล็อตที่pgslot42จะมีผลให้อะไรต่างๆที่ทำให้เว็บไซต์พวกนั้นมีความคุ้มราคาสำหรับผู้เล่นเยอะที่สุดในตอนนี้

  48. สำหรับผู้คนที่ชอบพอเล่นสล็อตและก็อยากได้สัมผัสประสบการณ์การpgzeedgameเล่นที่น่าเร้าใจรวมทั้งรื้นเริง ด้วยเว็บไซต์สล็อต

  49. ที่มากับระบบฝากถอนผ่าน25 รับ 100ที่ง่ายแล้วก็สบาย สำหรับผู้เล่นทุกคน ไม่ว่าจะเป็นนักเสี่ยงโชคมือใหม่หรือมือโปร

  50. ตัวเว็บไซต์เองมีเกมสล็อตที่นานาประการครอบคลุมทุกรส20 รับ 100แล้วก็สไตล์การเล่นทำให้มั่นใจว่าจะมีบางสิ่งที่ตรงกับความชื่นชอบของทุกคน.การลงทะเบียนสมัครสมาชิกเกิดเรื่องที่ง่ายสุดๆ

  51. เว็บไซต์พวกเรามีค่ายเกมให้เลือกเล่นได้แบบจุใจ มีเกมมากยิ่งกว่าพันเกม โปร 25 รับ 100ถอน ไม่ยั้ง สนุกสนานตื่นตาตื่นใจไปกับเอฟเฟคเกมสล็อตที่บันเทิงใจตื่นเต้น

  52. มีต้นแบบเกมมาฝาก อย่าลืมที่จะรอให้ท่านรื้นเริงpgslot42ชนะรางวัลครั้งใดก็ตามทำสิ่งพิเศษต่างๆที่มากขึ้นความมันส์ที่กำลังจะได้รับ ดังเช่น โบนัสเกมฟรีพิเศษสุดที่ชำระเงินมาแล้ว

  53. ความปลอดภัยและก็ความน่านับถือpgslot42 คุณรู้เรื่องกรรมวิธีการสำรวจความปลอดภัยแล้วก็ความน่านับถือของเว็บสล็อต ตัวอย่างเช่น การมีใบอนุมัติจากหน่วยงานการันตี

  54. พรีเซ็นท์ประสบการณ์การเล่นเกมสล็อตที่ไม่ซ้ำใคร9 รับ 100 ผสมระหว่างการเล่นที่มันและก็โบนัสที่ยิ่งใหญ่

  55. จากค่ายอื่นๆpgzeedgame การออกแบบเกมที่ประดิษฐ์ รวมทั้งช่องทางสำหรับเพื่อการชนะที่สูง ทำให้นักเสี่ยงโชคได้โอกาสได้มั่งคั่งจากการเล่นเกมสล็อต

  56. มีความมากมายหลายแล้วก็จำนวนมากให้เลือกเล่น 25 รับ 100โดยมีความไม่เหมือนในเรื่องของธีมแล้วก็ต้นแบบ ทุกคนสามารถคาดหมายสำหรับในการตื่นเต้นรวมทั้งโดยได้โอกาสได้รับรางวัลใหญ่โดยไม่จำเป็นต้องใช้ทุนมากมาย

  57. Needed to post you this bit of remark to finally say thanks a lot yet again for those unique tricks you have discussed here. It’s quite tremendously generous with you to allow publicly what a few people would’ve marketed for an e-book to make some profit for themselves, specifically seeing that you could have tried it in case you considered necessary. The pointers as well served like the easy way to comprehend many people have the identical eagerness like my own to learn significantly more with respect to this problem. I’m certain there are thousands of more fun instances ahead for many who go through your site.

  58. My spouse and i felt fortunate Raymond managed to round up his investigations out of the ideas he acquired out of the web site. It is now and again perplexing to simply always be freely giving points that many many others could have been selling. We really consider we need the blog owner to give thanks to because of that. All the explanations you made, the simple web site menu, the friendships your site make it possible to instill – it’s most excellent, and it’s helping our son and the family reckon that this situation is entertaining, and that is incredibly vital. Many thanks for the whole lot!

  59. I have to voice my admiration for your kind-heartedness giving support to those people who should have help on the content. Your special dedication to getting the message up and down turned out to be incredibly insightful and has truly allowed guys and women much like me to get to their dreams. Your entire insightful hints and tips entails this much a person like me and even further to my peers. Thanks a lot; from all of us.

  60. }{Always remember that there are tons of different options available to you when looking for hair accessories. The sky is really the limit here, and you can choose from different braids and headbands, bows and ribbons, clips and curlers, and much, much more. Your wardrobe should include several hair accessories. A simple ponytail holder goes great with an athletic suit. So before you go out, make sure you’re carrying a few accessories with you.

  61. I wish to convey my admiration for your kind-heartedness supporting men and women that actually need assistance with this one idea. Your real dedication to getting the message throughout had become exceedingly interesting and has made people like me to reach their desired goals. Your amazing important advice signifies much to me and much more to my office workers. With thanks; from each one of us.

  62. I would like to express my admiration for your kind-heartedness in support of women who require assistance with in this field. Your personal commitment to passing the solution all-around came to be remarkably valuable and have helped workers like me to arrive at their dreams. Your personal invaluable information denotes much to me and especially to my office workers. Warm regards; from everyone of us.

Leave a Reply to hi star golden goose Cancel reply

Your email address will not be published. Required fields are marked *