<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aditya Fulzele</title>
    <description>The latest articles on DEV Community by Aditya Fulzele (@aditya210399).</description>
    <link>https://dev.to/aditya210399</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1609256%2F20ab4b8b-7895-44cc-b7a3-7a2a40ed6635.png</url>
      <title>DEV Community: Aditya Fulzele</title>
      <link>https://dev.to/aditya210399</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aditya210399"/>
    <language>en</language>
    <item>
      <title>LeetCode Problem 217: Contains Duplicate</title>
      <dc:creator>Aditya Fulzele</dc:creator>
      <pubDate>Tue, 11 Jun 2024 10:38:53 +0000</pubDate>
      <link>https://dev.to/aditya210399/leetcode-problem-217-contains-duplicate-491b</link>
      <guid>https://dev.to/aditya210399/leetcode-problem-217-contains-duplicate-491b</guid>
      <description>&lt;h3&gt;
  
  
  Checking for Duplicate Elements in an Array
&lt;/h3&gt;

&lt;p&gt;Let's solve the problem step-by-step, providing a detailed explanation, code implementation, and complexity analysis.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem Statement
&lt;/h3&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; if any value appears at least twice in the array, and return &lt;code&gt;false&lt;/code&gt; if every element is distinct.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,3,1]
Output: true
Explanation: The number 1 appears twice in the array.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,3,4]
Output: false
Explanation: All elements are distinct.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true
Explanation: The number 1 appears multiple times, as do the numbers 3, 4, and 2.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Using a Set
&lt;/h4&gt;

&lt;p&gt;Using a set to check for duplicates is a straightforward and efficient approach. Sets do not allow duplicate elements, so we can utilize this property to solve the problem.&lt;/p&gt;

&lt;h5&gt;
  
  
  Algorithm
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Initialize an empty set.&lt;/li&gt;
&lt;li&gt;Iterate through each element in the array.&lt;/li&gt;
&lt;li&gt;For each element, check if it is already in the set.&lt;/li&gt;
&lt;li&gt;If it is, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If not, add the element to the set.&lt;/li&gt;
&lt;li&gt;If the loop completes without finding a duplicate, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;containsDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;seen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Complexity Analysis
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; (O(n)) - where (n) is the number of elements in the array. Each lookup and insertion in a set has an average time complexity of (O(1)).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; (O(n)) - in the worst case, if there are no duplicates, the set will contain all (n) elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Sorting the Array
&lt;/h4&gt;

&lt;p&gt;Sorting the array first can help in identifying duplicates. After sorting, any duplicate elements will be adjacent to each other.&lt;/p&gt;

&lt;h5&gt;
  
  
  Algorithm
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Sort the array.&lt;/li&gt;
&lt;li&gt;Iterate through the array.&lt;/li&gt;
&lt;li&gt;Check if the current element is the same as the next element.&lt;/li&gt;
&lt;li&gt;If it is, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the loop completes without finding duplicates, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;containsDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Complexity Analysis
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; (O(n \log n)) - due to the sorting step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; (O(1)) - if sorting in place, or (O(n)) - depending on the sorting algorithm used.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Using a Dictionary
&lt;/h4&gt;

&lt;p&gt;Using a dictionary (or hashmap) to count occurrences of elements is another effective method.&lt;/p&gt;

&lt;h5&gt;
  
  
  Algorithm
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Initialize an empty dictionary.&lt;/li&gt;
&lt;li&gt;Iterate through each element in the array.&lt;/li&gt;
&lt;li&gt;For each element, check if it is already a key in the dictionary.&lt;/li&gt;
&lt;li&gt;If it is, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If not, add the element to the dictionary with a value of 1.&lt;/li&gt;
&lt;li&gt;If the loop completes without finding a duplicate, return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;containsDuplicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Complexity Analysis
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; (O(n)) - each lookup and insertion in a dictionary has an average time complexity of (O(1)).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; (O(n)) - in the worst case, if there are no duplicates, the dictionary will contain all (n) elements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, we explored three different approaches to solve the problem of detecting duplicates in an array:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using a set&lt;/li&gt;
&lt;li&gt;Sorting the array&lt;/li&gt;
&lt;li&gt;Using a dictionary&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The set and dictionary approaches both offer (O(n)) time complexity and are straightforward to implement. Sorting the array, while providing an (O(n \log n)) solution, can be less optimal but still effective. Depending on the specific constraints and requirements of your application, any of these methods can be a suitable choice.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
