{"id":6466,"date":"2022-09-03T10:21:54","date_gmt":"2022-09-03T04:51:54","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/"},"modified":"2022-09-03T10:21:54","modified_gmt":"2022-09-03T04:51:54","slug":"solved-return-the-smallest-unique-value-in-a-given-array-c-language","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/","title":{"rendered":"[Solved] Return the smallest unique value in a given array &#8211; C Language"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-58460043\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"58460043\" data-parentid=\"58459539\" data-score=\"1\" 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>Using the algorithm you propose in your question (i.e. to first find all unique values and then find the lowest one), a possible implementation looks like this:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ NOTE: The second parameter array_size specifies the number of\n\/\/ int elements in the array, NOT the size of the array in bytes.\nint find_unique_min( int *array_start, int array_size )\n{\n    int *unique_values; \/\/pointer to the dynamically allocated array with the unique values\n    int num_unique_values; \/\/number of values in the secondary array\n    int minimum; \/\/smallest unique value found so far\n    int i, j; \/\/loop counter variables\n\n    \/\/ We don't know yet how many unique values there will be, so we don't know\n    \/\/ how big the secondary array which holds the unique values must be.\n    \/\/ However, in the worst case, every value is unique, which means that in\n    \/\/ the worst case, the secondary array must have the same size as the \n    \/\/ primary array. Therefore, we make it the same size.\n    unique_values = malloc( array_size * sizeof( int ) );\n    if ( unique_values == NULL ) {\n        fprintf( stderr, \"Memory allocation error\\n\" );\n        return -1;\n    }\n\n    \/\/ This variable specifies the number of valid elements in the\n    \/\/ secondary array, which must be set to zero for now.\n    num_unique_values = 0;\n\n    \/\/fill secondary array with unique values\n    for ( i = 0; i &lt; array_size; i++ )\n    {\n        \/\/ compare the current array element with all other elements\n        for ( j = 0; j &lt; array_size; j++ )\n        {\n            \/\/ Since the comparison will say the values are identical when\n            \/\/ comparing an element with itself, we must also check whether we\n            \/\/ are comparing the element with itself.\n            if ( array_start[i] == array_start[j] &amp;&amp; i != j ) goto not_unique;\n        }\n\n        \/\/ We have now determined that the current array element is unique,\n        \/\/ so we add it to the secondary array.\n        unique_values[num_unique_values++] = array_start[i];\n\nnot_unique:\n        continue;\n    }\n\n    \/\/return -1 if no unique values were found\n    if ( num_unique_values == 0 )\n    {\n        free( unique_values );\n        return -1;\n    }\n\n    \/\/find lowest value in secondary array and return it\n    minimum = INT_MAX;\n    for ( i = 0; i &lt; num_unique_values; i++ )\n    {\n        if ( unique_values[i] &lt; minimum ) minimum = unique_values[i];\n    }\n\n    free( unique_values );\n\n    return minimum;\n}\n<\/code><\/pre>\n<p>The only reason I included <code>&lt;stdlib.h&gt;<\/code> (which seems to be forbidden by the rules of your assignment) was because I needed to dynamically allocate memory for the secondary array to store the unique values. If you allocate a static array instead, you can get rid of that include directive. However, for a static array, the maximum number of unique numbers must be known at compile time (in order to determine how much memory to allocate).<\/p>\n<p>Alternatively, you can combine both steps (finding the unique values and finding the minimum value) into one. That way, you don&#8217;t need a secondary array and you also don&#8217;t need to (dynamically) allocate memory for it. In that case, you also don&#8217;t have to <code>#include &lt;stdlib.h&gt;<\/code>.<\/p>\n<p>Combining both steps could be done using the following algorithm:<\/p>\n<p>You can just go through the whole array from start to end (in a loop) and always remember (in a variable) the lowest unique value you have encountered so far. Whenever you encounter a lower value, you compare this value with all other elements of the array in order to determine whether it is unique. If it indeed is unique, you remember this new value instead of the previous one and continue with the rest of the array. At the end of the loop, you simply return the value of the variable which contains the lowest unique value you encountered.<\/p>\n<p>An implementation of this algorithm could look like this:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n\n#ifndef INT_MAX\n#define INT_MAX 2147483647\n#endif\n\n\/\/ NOTE: The second parameter array_size specifies the number of\n\/\/ int elements in the array, NOT the size of the array in bytes.\nint find_unique_min( int *array_start, int array_size )\n{\n    int minimum; \/\/smallest unique value found so far\n    int i, j; \/\/loop counter variables\n\n    minimum = INT_MAX;\n\n    for ( i = 0; i &lt; array_size; i++ )\n    {\n        if ( array_start[i] &lt; minimum )\n        {\n            \/\/ test if current element value is unique\n            for ( j = 0; j &lt; array_size; j++ )\n            {\n                if ( array_start[i] == array_start[j] &amp;&amp; i != j ) goto not_unique;\n            }\n\n            minimum = array_start[i];\n        }\nnot_unique:\n        continue;\n    }\n\n    \/\/if no unique value was found, return -1\n    if ( minimum == INT_MAX ) return -1;\n\n    return minimum;\n}\n<\/code><\/pre>\n<p>Please note that normally, <code>INT_MAX<\/code> should not be defined manually. The only reason I did this is because of the restriction mentioned in the question which prevented <code>#include &lt;stdlib.h&gt;<\/code> from being used.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Return the smallest unique value in a given array &#8211; C Language <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Using the algorithm you propose in your question (i.e. to first find all unique values and then find the lowest one), a possible implementation looks like this: #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; \/\/ NOTE: The second parameter array_size specifies the number of \/\/ int elements in the array, NOT the size of the array in &#8230; <a title=\"[Solved] Return the smallest unique value in a given array &#8211; C Language\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\" aria-label=\"More on [Solved] Return the smallest unique value in a given array &#8211; C Language\">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":[457,361,324],"class_list":["post-6466","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-arrays","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Return the smallest unique value in a given array - C Language - 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-return-the-smallest-unique-value-in-a-given-array-c-language\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Return the smallest unique value in a given array - C Language - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Using the algorithm you propose in your question (i.e. to first find all unique values and then find the lowest one), a possible implementation looks like this: #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; \/\/ NOTE: The second parameter array_size specifies the number of \/\/ int elements in the array, NOT the size of the array in ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-03T04:51:54+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=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Return the smallest unique value in a given array &#8211; C Language\",\"datePublished\":\"2022-09-03T04:51:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\"},\"wordCount\":325,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"arrays\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\",\"name\":\"[Solved] Return the smallest unique value in a given array - C Language - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-03T04:51:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Return the smallest unique value in a given array &#8211; C Language\"}]},{\"@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=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Return the smallest unique value in a given array - C Language - 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-return-the-smallest-unique-value-in-a-given-array-c-language\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Return the smallest unique value in a given array - C Language - JassWeb","og_description":"[ad_1] Using the algorithm you propose in your question (i.e. to first find all unique values and then find the lowest one), a possible implementation looks like this: #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; \/\/ NOTE: The second parameter array_size specifies the number of \/\/ int elements in the array, NOT the size of the array in ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/","og_site_name":"JassWeb","article_published_time":"2022-09-03T04:51:54+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Return the smallest unique value in a given array &#8211; C Language","datePublished":"2022-09-03T04:51:54+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/"},"wordCount":325,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","arrays","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/","url":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/","name":"[Solved] Return the smallest unique value in a given array - C Language - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-03T04:51:54+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-return-the-smallest-unique-value-in-a-given-array-c-language\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Return the smallest unique value in a given array &#8211; C Language"}]},{"@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=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","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\/6466","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=6466"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6466\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=6466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=6466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=6466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}