How to remove duplicates from string array in C#

2 Answers

0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        string[] arr = { "c#", "c#", "c", "c++", "php", "c", "c", "java" };
        arr = arr.Distinct().ToArray();

        Array.ForEach(arr, s => Console.WriteLine(s));
    }
}



/*
run:

c#
c
c++
php
java

*/

 



answered Sep 22, 2020 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;
 
class Program
{
    static void Main() {
        string[] arr = { "c#", "c#", "c", "c++", "php", "c", "c", "java" };

        var hash = new HashSet<string>(arr);
        
        arr = hash.ToArray();
        
        Console.WriteLine(string.Join(", ", arr));
    }
}
 
 
 
/*
run:
 
c#, c, c++, php, java
 
*/

 



answered Jul 18, 2022 by avibootz

Related questions

1 answer 157 views
1 answer 80 views
2 answers 169 views
169 views asked Mar 19, 2023 by avibootz
1 answer 126 views
1 answer 221 views
...