This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Most efficient way to remove duplicates from a List
(1 answer)
Closed 6 years ago.
I have an array of about 1000 words. I want to transfer them to another array while removing any duplicates. How can I do this in C# ?
The question almost is the answer:
var array2 = array1.Distinct().ToArray();
Related
This question already has answers here:
How can I efficiently remove elements by index from a very large list?
(6 answers)
Remove oldest n Items from List using C#
(6 answers)
Remove all but the first item in a list
(2 answers)
How to remove selected array values in C#
(5 answers)
Move First 10 List Items to Another Item List
(5 answers)
Closed 1 year ago.
This is what I have but there isn't anything that talks about how to delete multiple specific lines, for example, from line 2 - 8.
var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location
You can use RemoveRange because you have converted your text file into a list of strings.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0
You're best option is to do :
file.RemoveRange(2, 6);
This question already has answers here:
change array size
(15 answers)
Closed 3 years ago.
I want an array capacity to increase as I add items in order to add as many items to it as possible. Is there a way to do this in C#?
In C#, arrays are fixed lenght. The only way would be to create a new array, copy the contents and add the data.
Or just use List<T> or any other self-growing collection instead. They deal with exactly that part of the plumbing.
Is this what you're after?
Array.Resize(ref YourArray, i + 1);
This question already has answers here:
How do I generate a random integer in C#?
(31 answers)
How to get a random number from a range, excluding some values
(11 answers)
Select from a range but exclude certain numbers [duplicate]
(7 answers)
Closed 5 years ago.
I know this question sounds like a duplicate, but I haven't found an answer to it yet. Let me know, thanks.
For example:
a = 50
b = random
But b can never be the same as a
This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to remove elements from an array
I have an List with a bunch of data in it. Some of the lines start with a #. I want to remove those lines.
How...?
assuming its a string List
myList.RemoveAll(x => x.BeginsWith("#"));