{"id":25642,"date":"2022-12-11T12:56:50","date_gmt":"2022-12-11T07:26:50","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/"},"modified":"2022-12-11T12:56:50","modified_gmt":"2022-12-11T07:26:50","slug":"solved-duplicates-reduction","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/","title":{"rendered":"[Solved] Duplicates Reduction"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49820826\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49820826\" data-parentid=\"49820238\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Let&#8217;s get issue simple. We don&#8217;t need to delete duplicate character. We just remove the charachter at index 2. The string is &#8220;abcde&#8221;.<\/p>\n<pre><code>StringBuilder sb = new StringBuilder(\"abcde\");\nchar one = sb.charAt(2); \/\/ comment1, we know it's 'c'\nsb = sb.deleteCharAt(2); \/\/ comment2, let's delete 'c'. What would \"abcde\" be?\n\/\/ It's so ugly to be \"ab de\", so designer makes it be \"abde\"\nchar two = sb.charAt(2); \/\/ comment3, now you will know the result ^v^\n<\/code><\/pre>\n<p>For comment2, how does it make &#8220;abcde&#8221; to &#8220;abde&#8221;? Let&#8217;s look into source of StringBuilder. StringBuilder is a subClass of AbstractStringBuilder. And deleteCharAt(int index) is implemented in AbstractStringBuilder.<\/p>\n<pre><code>public AbstractStringBuilder deleteCharAt(int index) {\n    if ((index &lt; 0) || (index &gt;= count))\n        throw new StringIndexOutOfBoundsException(index);\n    System.arraycopy(value, index+1, value, index, count-index-1);\n    count--;\n    return this;\n}\n<\/code><\/pre>\n<p>Let&#8217;s omite index validation. What does <code>System.arraycopy(value, index+1, value, index, count-index-1);<\/code> do? You can find it&#8217;s doc <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-\">here<\/a>.<\/p>\n<pre><code>public static void arraycopy(Object src,\n                             int srcPos,\n                             Object dest,\n                             int destPos,\n                             int length)\n<\/code><\/pre>\n<blockquote>\n<p>Copies an array from the specified source array (which is &#8220;abcde&#8221;), beginning at the<br \/>\n  specified position (<code>index+1<\/code>, which is 3), to the specified position (<code>index<\/code>, which is 2) of the destination<br \/>\n  array (which is &#8220;abcde&#8221; too). A subsequence of array components are copied from the source<br \/>\n  array referenced by <code>src<\/code> to the destination array referenced by <code>dest<\/code> (<code>src<\/code> and <code>dest<\/code> are the same here, &#8220;abcde&#8221;).<br \/>\n  The number of components copied is equal to the <code>length<\/code> argument (<code>length<\/code> here is 5-2-1, it&#8217;s 2. So it will copy 2 chars). The<br \/>\n  components at positions <code>srcPos<\/code> through <code>srcPos+length-1<\/code> in the source<br \/>\n  array are copied into positions <code>destPos<\/code> through <code>destPos+length-1<\/code> (<code>srcPos<\/code> here is 2+1, it&#8217;s 3. <code>srcPos+length-1<\/code> is 3+2-1, it&#8217;s 4),<br \/>\n  respectively, of the destination array.<\/p>\n<\/blockquote>\n<p>So <code>System.arraycopy(value, index+1, value, index, count-index-1);<\/code> means copy 2 chars from index 3, to override index 2, override from index 2 to 3. Copy &#8216;de&#8217; to override &#8216;cd&#8217;. So after <code>System.arraycopy(value, index+1, value, index, count-index-1);<\/code>, makes <code>sb.value<\/code> to be &#8220;abdee&#8221;(Delete &#8216;l&#8217; already.). then count&#8211;, means &#8220;abdee&#8221; remove last char, so it becomes &#8220;abde&#8221;. <\/p>\n<p><code>will the next \"l\" takes position of the previous one after deleting it?<\/code> Yes, it will.<\/p>\n<p>See? Coding is not so hard and not so mystic. And the direct way is code yourself and run it.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Duplicates Reduction <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Let&#8217;s get issue simple. We don&#8217;t need to delete duplicate character. We just remove the charachter at index 2. The string is &#8220;abcde&#8221;. StringBuilder sb = new StringBuilder(&#8220;abcde&#8221;); char one = sb.charAt(2); \/\/ comment1, we know it&#8217;s &#8216;c&#8217; sb = sb.deleteCharAt(2); \/\/ comment2, let&#8217;s delete &#8216;c&#8217;. What would &#8220;abcde&#8221; be? \/\/ It&#8217;s so ugly &#8230; <a title=\"[Solved] Duplicates Reduction\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\" aria-label=\"More on [Solved] Duplicates Reduction\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323],"class_list":["post-25642","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Duplicates Reduction - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Duplicates Reduction - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Let&#8217;s get issue simple. We don&#8217;t need to delete duplicate character. We just remove the charachter at index 2. The string is &#8220;abcde&#8221;. StringBuilder sb = new StringBuilder(&quot;abcde&quot;); char one = sb.charAt(2); \/\/ comment1, we know it&#039;s &#039;c&#039; sb = sb.deleteCharAt(2); \/\/ comment2, let&#039;s delete &#039;c&#039;. What would &quot;abcde&quot; be? \/\/ It&#039;s so ugly ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-11T07:26:50+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Duplicates Reduction\",\"datePublished\":\"2022-12-11T07:26:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\"},\"wordCount\":243,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\",\"name\":\"[Solved] Duplicates Reduction - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-11T07:26:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Duplicates Reduction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Duplicates Reduction - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Duplicates Reduction - JassWeb","og_description":"[ad_1] Let&#8217;s get issue simple. We don&#8217;t need to delete duplicate character. We just remove the charachter at index 2. The string is &#8220;abcde&#8221;. StringBuilder sb = new StringBuilder(\"abcde\"); char one = sb.charAt(2); \/\/ comment1, we know it's 'c' sb = sb.deleteCharAt(2); \/\/ comment2, let's delete 'c'. What would \"abcde\" be? \/\/ It's so ugly ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/","og_site_name":"JassWeb","article_published_time":"2022-12-11T07:26:50+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Duplicates Reduction","datePublished":"2022-12-11T07:26:50+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/"},"wordCount":243,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/","url":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/","name":"[Solved] Duplicates Reduction - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-11T07:26:50+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-duplicates-reduction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Duplicates Reduction"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=25642"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25642\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=25642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=25642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=25642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}