elena1234

How to remove only the first instance of element in array

Dec 4th, 2021 (edited)
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace FirstInstance
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] numbers = { 1, 3, 4, 9, 2, 4 };
  11.             int numToRemove = 4;
  12.             int numIndex = Array.IndexOf(numbers, numToRemove);
  13.             numbers = numbers.Where((val, idx) => idx != numIndex).ToArray();
  14.         }
  15.     }
  16. }
  17.  
Add Comment
Please, Sign In to add comment