Skip to main content

Command Palette

Search for a command to run...

LeetCode: Contains Duplicate(JAVA)

5 min read

Published
•1 min read•View as Markdown
LeetCode: Contains Duplicate(JAVA)
M
Software Engineer with 5+ years of experience designing and building clinical-grade systems, including Electronic Medical Records (EMR), medical imaging tools, and scalable backend infrastructures. I specialize in developing end-to-end healthcare solutions using Python and Django REST Framework, with a strong focus on clinical workflow automation, multilingual systems, and high-performance data handling. My work includes building modular EMR platforms, prescription engines, and hybrid search systems for accurate medical data retrieval. I have also worked on medical imaging applications, integrating Python-based tools with analysis modules to support diagnostic workflows and image-based decision-making.

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

This problem like most other problems has multiple solutions.

The easiest way to detect non-unique values is by using the bruteforce approach, we take the first element of the array and compare it to the other elements, and we will repeat thesame process for all the elements in the array. this approach will give us a time complexity of O(n^2) and space of O(1) since we do not need extra space for this. This is not a bad solution, the question can we do better?

We can use a HashSet, you might ask why a HashSet, a HashSet will not allow duplicate values.

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> uniqueValues = new HashSet<>();
         for(int i=0; i<nums.length; i++){ 
             if(uniqueValues.contains(nums[i])){
                 return true;
             }
            uniques.add(nums[i]);

                }
           return false;     
    }

}

Time: O(n) Space: O(n)

You can comment with solutions for different languages

More from this blog

eikon

17 posts

Hello! I am Monyuy a software engineer. A day in my life consists of prepping web art, fixing front end bugs, adding API endpoint, database design and blogging.