---
title: "Contain Duplicate"  
description: "Contain Duplicate"  
author: "Steilla Mitchel"  
published: 2024-06-12  
updated: 2024-06-12  
canonical: https://www.mindstick.com/forum/160730/contain-duplicate  
category: "c#"  
tags: ["c#", "programming language", "programming help", "programs"]  
reading_time: 2 minutes  

---

# Contain Duplicate

Given an [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) `nums`, return `true` if any [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) appears **at [least](https://yourviews.mindstick.com/story/1471/5-autobiographies-you-should-read-at-least-once) twice** in the array, and return `false` if every element is distinct.

## Replies

### Reply by Ravi Vishwakarma

Let's write code to check whether the array contains duplicate values.

```cs
// Method to determine if the array contains any duplicates
public static bool ContainsDuplicate(int[] nums)
{
    // Create a list to keep track of the elements we have seen
    IList<int> list = new List<int>();

    // Iterate through each element in the array
    foreach (var item in nums)
    {
        // Check if the current element already exists in the list
        if (list.Contains(item))
        {
            // If it does, return true indicating a duplicate is found
            return true;
        }
        else
        {
            // If it doesn't, add the element to the list
            list.Add(item);
        }
    }

    // If no duplicates are found after checking all elements, return false
    return false;
}
```

Now call this function from the main function.

```cs
    public class Program
    {
        public static void Main()
        {
            IList<int[]> list = new List<int[]>
            {
                new int[] { 1, 2, 3, 1 },
                new int[] { 1, 2, 3, 4 },
                new int[] { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 }
            };

            foreach (var item in list)
            {
                Console.WriteLine(item + " " + Program.ContainsDuplicate(item));
            }

            Console.ReadLine();
        }
    }
```

## Output :

```plaintext
System.Int32[] True
System.Int32[] False
System.Int32[] True
```


---

Original Source: https://www.mindstick.com/forum/160730/contain-duplicate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
