217 Contains Duplicate · LeetCode Solutions.

217. Contains Duplicate

Problem

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.

Process

  • Create an empty hashSet.
  • Loop through a given array and set every element of that array to the hashSet.
  • Check if the length of the array matches the count of hashSet.
  • If it matches return false else return true.

Solution

using System.Collections.Generic;
public class Solution {
    public bool ContainsDuplicate(int[] nums) {
        HashSet<int> has = new HashSet<int>();

        for(int i=0;i<nums.Length;i++){
            has.Add(nums[i]);
        }
        return has.Count != nums.Length;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *