How to remove duplicate strings from array of string in C#

3 Answers

0 votes
using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static void Main() {
        string[] arr = {"c#", "php", "c#", "java", "php"};
 
        var hs = new HashSet<string>(arr);
 
        arr = hs.ToArray();
         
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine(arr[i]);
        }
    }
}
 
 
 
/*
run:
 
c#
php
java
 
*/

 



answered Oct 28, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        string[] arr = {"c#", "php", "c#", "java", "php"};
 
        var hs = new HashSet<string>(arr);
        
        Array.Clear(arr, 0, arr.Length);
 
        hs.CopyTo(arr);
         
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine(arr[i]);
        }
    }
}
 
 
 
/*
run:
 
c#
php
java
 
*/

 



answered Oct 28, 2019 by avibootz
0 votes
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main() {
        string[] arr = {"c#", "php", "c#", "java", "php"};
 
        arr = arr.Distinct().ToArray();
         
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine(arr[i]);
        }
    }
}
 
 
 
/*
run:
 
c#
php
java
 
*/

 



answered Oct 28, 2019 by avibootz
...