Data Structure #01

Data Structure #01

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.

?

Example 1:

Input: nums = [1,2,3,1]
Output: true
        

Example 2:

Input: nums = [1,2,3,4]
Output: false
        

Solution

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> intset = new HashSet<>();
        for(int num : nums){
            if(intset.contains(num))
            return true;
            intset.add(num);    
        }
        return false;
}
}        


要查看或添加评论,请登录

Nilu Kumari的更多文章

  • Binary Tree:-

    Binary Tree:-

    Binary:- A binary tree is a recursive data structure where each node can have two children at most. A common type of…

  • Comparable & Comparator in Java

    Comparable & Comparator in Java

    Comparable & Comparator both are interfaces and can be used to sort collection elements. Comparable provides a single…

  • Queue Implementation Using LinkedList

    Queue Implementation Using LinkedList

    Queue:- A queue is defined as a linear data structure that is open at both ends and the operations are performed in…

  • Wrapper Classes

    Wrapper Classes

    Wrapper classes provides the mechanism to convert primitive into object and object into primitive. There are two way to…

  • Doubly Linked List

    Doubly Linked List

    Doubly Linked List:- A doubly linked list is a special type of linked list in which each node contains a pointer to the…

  • LinkedList Implementation In detail.

    LinkedList Implementation In detail.

    LinkedList--> LinkedList is a part of the Collection Framework present in java.util package.

  • Composition in Java

    Composition in Java

    Composition:-composition represents a stronger relationship where one class is composed of one or more other classes…

  • Aggregation in Java

    Aggregation in Java

    Association:- Association is to show the relationship between two or more class. It shows Has-a-relationship.

  • JDBC

    JDBC

    /* * JDBC: Java Database Connectivity * In real word we are seeing that some login page or register page * when a user…

  • Stack Implementation Using LinkedList

    Stack Implementation Using LinkedList

    public class Stack { static class Node{ int data; Node next; Node(int data){ this.data=data; this.

社区洞察

其他会员也浏览了