-1

I have a string[] with the same value but different index or location:

string[] test = {"jane", "joy", "adam", "jane"};

I want to delete the first jane without deleting the last jane:

string[] test = {"joy", "adam", "jane"};

I am using this method to delete:

string[] newTest = test.Where(w => w != test[0]).ToArray();

But that deletes all janes:

string[] newTest = {"joy", "adam"};
alex
  • 6,818
  • 9
  • 52
  • 103
Nyako
  • 11
  • 6

2 Answers2

1

You can find index of element first and then remove it from list

something like,

 string[] test = {"jane", "joy", "adam", "jane"};
 List<string> list = new List<string>(test);
 list.RemoveAt(list.FindIndex(x => x == "jane"));

 Console.WriteLine(string.Join(",", list));

If you just want to delete first element from an array then there are multiple ways to achieve this,

First :

string[] test = {"jane", "joy", "adam", "jane"};
var result = test.Skip(1);
//If you want to convert it to array then
//string[] result = test.Skip(1).ToArray();

Console.WriteLine(string.Join(",", result));

Second : @Caius suggested

string[] test = {"jane", "joy", "adam", "jane"};
var result = test.RemoveAt(0);

Check index in Where clause,

string[] test = {"jane", "joy", "adam", "jane"};
var result = test.Where((x, i) => i != 0);

POC : .net Fiddle

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

in my project i using this method to delete

But that's saying "I want a new array where no element is the word "jane" so it removes all of them

Here's one method to remove an arbitrary index from an array using a List:

List<string> l = new List<string>(test);
l.RemoveAt(0);
string[] newTest = l.ToArray();

To be honest, you might be better to avoid using an array all together, and just use a List as your storage device if you need this kind of edit

Consider also the advice found here: Remove element of a regular array


If your question is "I want to remove the first occurrence of jane but not the second", let's find the first jane, then make a new array that excludes it:

List<string> l = new List<string>(test.Length);
bool removing = true;
foreach(string s in test) {
  if(removing && s == "jane") 
    removing = false;
  else      
    l.Add(s);
}
string[] newTest = l.ToArray();

There are other ways; you could indexOf to find the first jane, and then Array.Copy everything up to but not including the first instance, and everything after, not including etc.. This code is fairly self documenting though - iterate the array, looking for jane, if we find her and we're still in removing mode, just turn removing mode off (this jane is then never added it to the list, and turning off the removing flag means we'll certainly add the rest of the names into the list) and carry on processing the rest of the list

Consider how much simpler this code would be though if you were just using a List as your storage container:

test.RemoveAt(test.IndexOf("jane"));
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Oh, so you didn't want to search for the first occurrence of "jane", you just wanted to delete the first element of the array? Psh. – Robert Harvey Aug 20 '19 at 13:47
  • no. i just want to delete specific array without delete same value in another location. so if i have 3 jane i only delete 1 jane in specific array and keep 2 jane – Nyako Aug 20 '19 at 13:54
  • I added some more to the answer for this – Caius Jard Aug 20 '19 at 13:55