{"id":3529,"date":"2019-03-08T00:01:06","date_gmt":"2019-03-08T08:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3529"},"modified":"2019-03-02T16:15:03","modified_gmt":"2019-03-03T00:15:03","slug":"find-the-duplicates-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3529","title":{"rendered":"Find the Duplicates &#8211; Solution"},"content":{"rendered":"<p>I devised two solutions for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3514\">this month&#8217;s Exercise<\/a>. The basic solution can be solved in two steps, with a more involved solution using three.<br \/>\n<!--more--><br \/>\nThe first step for both solutions is to fill the array and output its values. This process can be done in a single loop:<\/p>\n<pre class=\"screen\">\r\nfor(a=0;a&lt;SIZE;a++)\r\n{\r\n    array[a] = rand() % 50 + 10;\r\n    printf(\" %2d\",array[a]);\r\n}\r\nputchar('\\n');<\/pre>\n<p>For my first solution, the second step is to chomp through the array similar to a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1247\">bubble sort<\/a> to find duplicates.<\/p>\n<p>In the following snippet, a <em>while<\/em> loop processes <code>array[]<\/code> from element zero in variable <code>a<\/code> to the value of defined constant <code>SIZE<\/code>, which is set to 15:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* find and display duplicates *\/<\/span>\r\nputs(\"Duplicates:\");\r\na = 0;\r\nwhile( a &lt; SIZE-1 )\r\n{\r\n    for(b=a+1;b&lt;SIZE;b++)\r\n    {\r\n        if( array[a] == array[b] )\r\n        {\r\n            printf(\"%d\\n\",array[a]);\r\n            <span class=\"comments\">\/* no need to continue *\/<\/span>\r\n            break;\r\n        }\r\n    }\r\n    a++;\r\n}<\/pre>\n<p>Variable <code>b<\/code> scans elements from <code>a+1<\/code> through <code>SIZE<\/code> looking for matches. When one is found, it&#8217;s displayed and the <em>for<\/em> loop is broken. Then variable <code>a<\/code> is incremented and the process continues. Here&#8217;s sample output:<\/p>\n<p><code>Array:<br \/>\n&nbsp;26 59 47 41 37 21 47 42 50 28 30 14 21 32 22<br \/>\nDuplicates:<br \/>\n47<br \/>\n21<\/code><\/p>\n<p>And here&#8217;s the output when the same value is repeated more than twice:<\/p>\n<p><code>Array:<br \/>\n&nbsp;56 16 43 49 22 45 57 41 49 11 18 49 24 12 49<br \/>\nDuplicates:<br \/>\n49<br \/>\n49<br \/>\n49<\/code><\/p>\n<p>The code doesn&#8217;t recognize previously-reported values, so the output looks silly.<\/p>\n<p>For my second attempt, I decided to quicksort the array before I scanned for duplicates. With the array sorted, duplicates appear next to each other. Therefore, the loop to find repeated values works differently than the bubble-sort-like loop in my first solution:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* sort and display duplicates *\/<\/span>\r\nqsort(array,SIZE,sizeof(int),compare);\r\nputs(\"Duplicates:\");\r\nfor(a=0;a&lt;SIZE-1;a++)\r\n{\r\n    b = 1;\r\n    while( array[a] == array[a+b] )\r\n    {\r\n        d = array[a];\r\n        b++;\r\n        a++;\r\n        <span class=\"comments\">\/* prevent overflow *\/<\/span>\r\n        if( a + b &gt; SIZE)\r\n            break;\r\n    }\r\n    <span class=\"comments\">\/* b is &gt;1 only for duplicates *\/<\/span>\r\n    if( b &gt; 1 )\r\n        printf(\"%d\\n\",d);\r\n}<\/pre>\n<p>Variables <code>a<\/code> and <code>b<\/code> compare elements. The <em>for<\/em> loop propels variable <code>a<\/code> through the entire array. Variable <code>b<\/code> is set equal to 1 and a <em>while<\/em> loop compares <code>array[a]<\/code> and <code>array[a+b]<\/code>. If they match, the value of <code>array[a]<\/code> is saved in variable <code>d<\/code>. Both <code>a<\/code> and <code>b<\/code> are incremented to compare the next two elements, which might also be the same. Along the way, and <em>if<\/em> test confirms that <code>a+b<\/code> doesn&#8217;t reference elements beyond the end of the array.<\/p>\n<p>When the <em>while<\/em> loop is completed, an <em>if<\/em> statement checks the value of variable <code>b<\/code>, which will be greater than one when a duplicate is found. If so, the value (in variable <code>d<\/code>) is output:<\/p>\n<p><code>Array:<br \/>\n&nbsp;36 18 23 41 55 46 36 10 18 45 32 11 28 43 10<br \/>\nDuplicates:<br \/>\n10<br \/>\n18<br \/>\n36<\/code><\/p>\n<p>By sorting, I helped eliminate the problem reporting duplicate duplicates in my first solution.<\/p>\n<p>I hope you coded some clever solutions. Give yourself bonus points if you also counted the number of duplicates found.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_03-Exercise-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view my first solution on Github.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_03-Exercise-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">Click here<\/a> to view my second solution on Github.<\/p>\n<p>I&#8217;m using Github now to host code for this blog. The reasons is that WordPress&#8217;s latest security update prohibits me from uploading <code>*.c<\/code> files as media. I&#8217;ve tried working in some patches and permissions, but nothing has been effective. You&#8217;ll also find Lesson files on my Github page as well: <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/dangookin\/C-For-Dummies-Blog<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I devised two solutions for this month&#8217;s Exercise. The basic solution can be solved in two steps, with a more involved solution using three.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3529","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3529"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3529\/revisions"}],"predecessor-version":[{"id":3537,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3529\/revisions\/3537"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}