Remove duplicate elements from an array How to remove duplicate elements in a string array in C# 13-Jun-19 04:03 PM? I'll cover the following topics in the code samples below: NET FrameworkEventArgs, ArrayList, LINQ, ToArray, and CopyTo. I want to remove duplicate values from a string using VB.NET. If values = '3,40,15,109,15' then I need the end result to be '3,40,15,109'. I'm using the code below but receiving.
I build up the ItemList array with the above code. How do i remove any duplicates in this array?
6 Answers
You'd declare some array (or List, or whatever collection you may prefer) and would do something like:
Then you could use LINQ:
And then, iterate the array and do whatever you need.
EDIT: Better what Srinivasan__ said, check whether the item exists, and if it doesn't add it. To check for it, you can use Exists(). Or if using something like a List, Contains().
Rather than checking and removing the duplicate elements you can check whether it is in the array, if it doesn't exist you can add to array, else do nothing.
Declare a List<string>
object named for instance list
. In the loop:
I just saw this, I tried the following code ; it works fine, I'm using a CheckedListBox to view results.There 2 arraylist used. 'Darray' holding the original list of duplicated strings. Then, 'FinArray' to dump the non-duplicated strings, then display the 'FinArray' contents in the listbox: