217 Contains Duplicate · LeetCode Solutions.

217. Contains Duplicate


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.


  • 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.


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++){
        return has.Count != nums.Length;

Leave a Reply

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