{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>217. Contains Duplicate</h1>\n",
    "<hr>\n",
    "\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "<p>Given an array of integers, find if the array contains any duplicates.</p>\n",
    "\n",
    "<p>Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.</p>\n",
    "\n",
    "<p><strong>Example 1:</strong></p>\n",
    "\n",
    "<pre><strong>Input:</strong> [1,2,3,1]\n",
    "<strong>Output:</strong> true</pre>\n",
    "\n",
    "<p><strong>Example 2:</strong></p>\n",
    "\n",
    "<pre><strong>Input: </strong>[1,2,3,4]\n",
    "<strong>Output:</strong> false</pre>\n",
    "\n",
    "<p><strong>Example 3:</strong></p>\n",
    "\n",
    "<pre><strong>Input: </strong>[1,1,1,3,3,4,3,2,4,2]\n",
    "<strong>Output:</strong> true</pre>\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<a href=\"https://leetcode.com/problems/contains-duplicate/\">Source</a> \n",
    "<hr>\n",
    "\n",
    "<h4>Code</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def contains_duplicate(nums):\n",
    "    \"\"\"Using set()\n",
    "    Time Complexity: O(n)\n",
    "    Space Complexity: O(n)\n",
    "    \"\"\"\n",
    "    my_set = set()\n",
    "    for n in nums:\n",
    "        if n in my_set:\n",
    "            return True\n",
    "        my_set.add(n)\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def contains_duplicate(nums):\n",
    "    \"\"\"variation of above solution\"\"\"\n",
    "    return len(nums) != len(set(nums))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h4>Check</h4>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,1]\n",
    "contains_duplicate(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,4]\n",
    "contains_duplicate(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,1,1,3,3,4,3,2,4,2]\n",
    "contains_duplicate(nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "<h4>Follow up:</h4>\n",
    "<p>Can you solve it with O(1) Space Complexity (i.e. constant memory)?</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def contains_duplicate(nums):\n",
    "    \"\"\"using sort, and check consecutive items\n",
    "    Time Complexity: O(nlogn)\n",
    "    Space Complexity: O(1)\n",
    "    \"\"\"\n",
    "    if len(nums) < 2:\n",
    "        return False\n",
    "    nums.sort()\n",
    "    for i, num in enumerate(nums[:-1]):\n",
    "        if num == nums[i+1]:\n",
    "            return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,1]\n",
    "contains_duplicate(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,2,3,4]\n",
    "contains_duplicate(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = [1,1,1,3,3,4,3,2,4,2]\n",
    "contains_duplicate(nums)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}