Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Remove all duplicate elements from a given array

C# Sharp Array: Exercise-33 with Solution

Write a C# Sharp program to remove all duplicate elements from a given array and returns a new array.

Sample Solution:-

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
          {
            object[] mixedArray = new object[8];
            mixedArray[0] = 25;
            mixedArray[1] = "Anna";
            mixedArray[2] = false;
            mixedArray[3] = 25;
            mixedArray[4] = System.DateTime.Now;
            mixedArray[5] = 112.22;
            mixedArray[6] = "Anna";
            mixedArray[7] = false;
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < mixedArray.Length; i++)
            {
                Console.WriteLine(mixedArray[i]);
            }
            object[] result = test(mixedArray);
            Console.WriteLine("\nAfter removing duplicate elements from the said array:");
            for (int i = 0; i < result.Length; i++)
            {
                Console.WriteLine(result[i]);
            }

        }
        public static object[] test(object[] mixedArray)
        {
            return mixedArray.Distinct().ToArray();
        }
     }
}

Sample Output:

Original array elements:
25
Anna
False
25
4/15/2021 12:10:51 PM
112.22
Anna
False

After removing duplicate elements from the said array:
25
Anna
False
4/15/2021 12:10:51 PM
112.22
 

Flowchart:

Flowchart: Remove all duplicate elements from a given array.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C# Sharp program to get only the odd values from a given array of integers.
Next: Write a C# Sharp program to find the missing number in a given array of numbers between 10 and 20.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.