LeetCode Problem 217: Contains Duplicate (Python) I’m following the beginner LeetCode roadmap, and today I learned and completed problem #217 — Contains Duplicate. I used a set to achieve O(n) time complexity, with O(1) average-time lookups. The idea is simple: iterate through the list and add each number to a set. If you ever see a number that’s already in the set, you’ve found a duplicate, so you can stop immediately and return True. For example, if the list is [1, 3, 5, 3, 1], the set will grow to {1, 3, 5}. When you reach the 4th element (3), it’s already in the set, so the algorithm stops and returns True. Some of you might think it should keep going because there are two duplicates. That’s true, but for this problem you only need to know whether any duplicate exists, so stopping at the first one is enough.
LeetCode 217 Contains Duplicate Solution in Python
More Relevant Posts
-
For those that have errors in their #python code ;-) here a way to customize the output with colors and other stylistic elements: PrettyErrors https://lnkd.in/eK-4_-mM
To view or add a comment, sign in
-
I hid an Easter Egg in our tooling… It’s fast. In case you haven't heard, there is a new kid on the block doing fast type checking: ty https://docs.astral.sh/ty/ ty is a new type checker from the team behind ruff (the linter that already replaced flake8 and black for us). It's written in Rust. Same idea: take a critical Python dev tool and make it instant. Our Python codebase runs mypy on every commit and every PR. It works, but sometimes you see that 2-3 seconds wait that even on a mid-size project, it adds noticeable seconds to every pre-commit hook and CI run. Multiply that by every developer, every push, every day. What we did: 1. Tested removing the Pydantic mypy plugin, zero new errors. It wasn't catching anything our type annotations didn't already cover. 2. Ran ty alongside mypy to compare output. 99 initial errors, all from third-party library stub gaps (SQLAlchemy, pyarrow), not real bugs. 3. Added per-module overrides in pyproject.toml to suppress the stub noise, same thing we were doing with mypy's # type: ignore comments. 4. Swapped mypy for ty in pre-commit, CI, and our justfile. Removed mypy entirely. Result: Type checking went from "look out the window" to "already done." Same coverage, same confidence, faster feedback loop. Should you migrate today? If you rely heavily on mypy plugins (Pydantic), test without them first. If your codebase passes clean, you're good to go. If not, wait for ty's plugin ecosystem to mature. You could argue that ty is still early but is moving incredibly fast. For well-typed codebases, it's already a drop-in improvement. Migration inspired by https://lnkd.in/eu5iMfS2
All my Python projects are now using ty for type checks 🎉 FastAPI, Typer, SQLModel, Asyncer, etc. Thank you Sofie 🙌 ty is great, try it https://docs.astral.sh/ty/
To view or add a comment, sign in
-
VSCode workspace is a game changer! If you're writing Python codes for different versions (2.* and 3.*), then you should try the workspace in VSCode. Basically with workspace, you can define/set which CPython version to use when you click the play/run button in VSCode. This is very useful in cases where you might be trying out a logic in Python 2 and also on Python 3, or when you're migrating either upwards (from 2 to 3) or downwards (from 3 to 2), and since it's on the same IDE, the experience is really enjoyable. This also works on the same major version, so maybe you have Python 2.2 and 2.7, or 3.10 and 3.13; and it's not limited to just 2, you can have more. First time I tried it, had me smiling :) cause I work on different versions and it's not fun going to console and manually typing which python version to run on which folder --- (side note: python launcher is a great tool, but having an IDE that can support multiple versions seamlessly is just another level of ease). I really liked it, try it and see for yourself ;) #vscode #workspace #dcm #python
To view or add a comment, sign in
-
Let's now talk about Variables in Python. What is a variable? Think of it like a box, you give it a name and store a value inside it. Example: a = 10 name = 'Alice' price = 19.99 Here, a, name, price are all variables in which we have stored some value or data. Simple right? But here's where most beginners make mistakes- naming their variables wrong. There are 3 conventions you need to follow: 1️⃣ First letter should be lowercase (best practice as per PEP8) 2️⃣ Never start a variable name with a number 3️⃣ Never use spaces, use an underscore instead Break any of these and Python will throw an error before your code even runs. Get these right from day one and you'll save yourself a lot of frustration later. #Python #DataAnalytics #data #python #learnpython #dataanalyst #pythonforbeginners
To view or add a comment, sign in
-
-
If you know Python, you already have an unfair advantage in trading. Why? Because markets reward: -Speed -Discipline -Repeatable strategies All of which can be automated. The barrier to entry is much lower than most people think. Here’s a beginner-friendly way to get started: link in the comments section 👇
To view or add a comment, sign in
-
Copy Fail is 732 bytes of Python. An agent can fetch it and run it in one turn. That is the part that should worry us. The lesson is not only “patch the kernel.” It is that agent runtimes need to block the path from public text to execution. For this specific exploit path, two controls matter: Block su. Block AF_ALG. Prompt-level instructions can tell the model not to run exploits. Runtime policy can make the exploit path unavailable. Blog post link in the first comment 👇
To view or add a comment, sign in
-
-
This is exactly the kind of runtime problem I think we’ll see more often with agents. Copy Fail is a tiny public PoC. For a human, there is still friction between reading it and running it. For an agent, that distance can collapse to one turn. That means the right question is not only “did we patch?” It is also: Could the agent execute the dangerous path in the first place? That is where AgentSH fits: block su, block unnecessary socket families like AF_ALG, and enforce policy where the agent turns text into OS activity.
Copy Fail is 732 bytes of Python. An agent can fetch it and run it in one turn. That is the part that should worry us. The lesson is not only “patch the kernel.” It is that agent runtimes need to block the path from public text to execution. For this specific exploit path, two controls matter: Block su. Block AF_ALG. Prompt-level instructions can tell the model not to run exploits. Runtime policy can make the exploit path unavailable. Blog post link in the first comment 👇
To view or add a comment, sign in
-
-
Top 4 Python patterns every beginner should practice 🐍✨ Pattern programs are one of the best ways to understand nested loops, rows, columns, conditions, spacing, and how logic builds shapes step by step. In this post, you’ll see how Python can print: Heart pattern ❤️ Hollow square pattern ⬛ Pyramid pattern 🔺 Right triangle pattern 📐 These may look simple, but they train your brain to think like a programmer. You learn how loops move line by line. You learn how conditions control the output. You learn how spacing changes the entire shape. You learn how small logic creates a full pattern.
To view or add a comment, sign in
-
-
7 small steps to start with algorithmic trading: 1. Start with Python 2. Learn to use VSCode 3. Take a pandas tutorial 4. Then a plotly tutorial 5. Make a portfolio with riskfolio 6. Make a backtest with vectorbt 7. Analyze performance with vectorbt You can do this! Want to learn how? Register here: https://lnkd.in/e6MyxcJd
To view or add a comment, sign in
-
-
🚀 Learning Python the Practical Way: Understanding Virtual Environments Over the past few days, I started learning Python and decided to focus on building instead of just reading syntax. Today, I explored one of the most important concepts for any developer: Virtual Environments (venv) Here’s what I understood: 🔹 A virtual environment is an isolated Python setup for a specific project 🔹 It prevents version conflicts between different projects 🔹 Each project can have its own dependencies without affecting others 💡 Why it matters: While working on multiple projects, different versions of the same library can break things. Virtual environments solve this by keeping everything separate and controlled. 🛠️ What I practiced: Creating a virtual environment Activating and deactivating it Installing packages inside it Understanding how Python uses project-specific paths This concept is very similar to how we manage dependencies in Node.js projects, but implemented differently in Python. Next step: Building a simple backend server using FastAPI to apply this knowledge in real projects. #Python #BackendDevelopment #FastAPI #WebDevelopment #LearningInPublic
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development