0

Instead of removing the first occurrence of a duplicated value, as my code currently does, how can I get it to delete the second occurrence instead?

namespace deletenumber
{
 class Program
 { 
 public static void Main(String[] args)
 {
 Random r = new Random();
 int[] a = new int[10];
 for (int i = 0; i < 10; i++)
 a[i] = r.Next(1, 100);
 for (int i = 0; i < 10; i++)
 Console.WriteLine(" a [" + i + "] = " + a[i]);
 Console.WriteLine();
 Console.ReadLine();
 Console.WriteLine("What number do you want to delete?");
 int item = Convert.ToInt32(Console.ReadLine());
 Console.WriteLine();
 int index = Array.IndexOf(a, item);
 a = a.Where((b, c) => c != index).ToArray();
 Console.WriteLine(String.Join(", ", a));
 Console.ReadLine();
 }
 }
}
Arsen Khachaturyan
8,4304 gold badges46 silver badges47 bronze badges
asked Aug 17, 2020 at 16:18
4
  • 3
    You should always reveal when you are submitting homework as a task, you should show what you have tried, and how you are unit testing. You do none of this. Commented Aug 17, 2020 at 16:22
  • 3
    @MortenBork let's please be more patient against new users. This is why this post exist. Commented Aug 17, 2020 at 16:26
  • @McCloud54 please check this about how to ask questions in SO. Commented Aug 17, 2020 at 16:39
  • It is unknown what the real task is. You may be asked to actually remove the second instance of that number (not the third or however many more there might be), also you may be asked to do this using constant space, and also in the most efficient way. In that sense, the question leaves too much to the imagination. Commented Aug 17, 2020 at 16:43

2 Answers 2

1

I'm not sure why exactly you might want to delete only the second occurrence because there should be no matter which one of the duplicates is removed, correct?

Anyway, there are several ways of achieving what you want. I will not directly write a solution here, but will propose the methods you can use in order to achieve what you want:

  1. Array.LastIndexOf
  2. Use the same IndexOf method again to find the second occurrence, by starting the search from the previous occurrence (myArray.IndexOf("string", last_indexof_index);)

Generally, consider using a HashSet<T> (official docs here) if you want to have a collection of unique elements.

You can also convert your existing collection to HashSet<int>:

int[] a = new int[10];
var set = new HashSet<int>(a);
answered Aug 17, 2020 at 16:35
Sign up to request clarification or add additional context in comments.

1 Comment

Notice that the first solution will not remove the second occurrence but the last. So if you had 3 or more occurrences of the number it would always remove the last one but not explicitly the second.
1

You could convert your array to a List and then just remove the index before casting it back to an array:

var list = new List<int>(a);
list.RemoveAt(index);
a = list.ToArray();
answered Aug 17, 2020 at 16:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.