Home »
        VB.Net »
        VB.Net Programs
    
    VB.Net program to delete a given element from the one-dimensional array
    
    
    
    
	    
		    By Nidhi Last Updated : November 11, 2024
	    
    
    
    Delete a given element from an array
    Here, we will create an array of integers and then read elements from the user. After that, we will find and delete the item from the array.
    Program/Source Code:
    The source code to delete a given element from the one-dimensional array is given below. The given program is compiled and executed successfully.
    VB.Net code to delete a given element from an array
'VB.Net program to delete a given element 
'from the one-dimensional array.
Module Module1
    Sub Main()
        Dim arr As Integer() = New Integer(6) {}
        Dim flag As Integer = 0
        Dim item As Integer = 0
        Console.WriteLine("Enter array elements: ")
        For i = 0 To 5 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next
        Console.Write("Enter Item: ")
        item = Integer.Parse(Console.ReadLine())
        flag = 0
        For i = 0 To 5 Step 1
            If (arr(i) = item) Then
                flag = 1
                For j = i To 4 Step 1
                    arr(j) = arr(j + 1)
                Next
                GoTo OUT
            End If
        Next
OUT:
        If (flag) Then
            Console.WriteLine("Item {0} deleted successfully.", item)
        Else
            Console.WriteLine("{0} not found.", item)
        End If
        Console.WriteLine("Elements of array after deletion ")
        For i = 0 To (5 - flag) Step 1
            Console.WriteLine("{0}", arr(i))
        Next
    End Sub
End Module
Output
Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 40
Element[4]: 50
Element[5]: 60
Enter Item: 50
Item 50 deleted successfully.
Elements of array after deletion
10
20
30
40
60
Press any key to continue . . .
    Explanation
    In the above program, we created a module Module1 that contains a function Main(). In the Main(), we created an array arr of six elements and we also created more variables item, flag which are initialized with 0.
Console.WriteLine("Enter array elements: ")
For i = 0 To 5 Step 1
    Console.Write("Element[{0}]: ", i)
    arr(i) = Integer.Parse(Console.ReadLine())
Next
    In the above code, we read the values of array elements from the user.
flag = 0
For i = 0 To 5 Step 1
    If (arr(i) = item) Then
        flag = 1
        For j = i To 4 Step 1
            arr(j) = arr(j + 1)
        Next
        GoTo OUT
    End If
Next
OUT:
If (flag) Then
    Console.WriteLine("Item {0} deleted successfully.", item)
Else
    Console.WriteLine("{0} not found.", item)
End If
Console.WriteLine("Elements of array after deletion ")
For i = 0 To (5 - flag) Step 1
    Console.WriteLine("{0}", arr(i))
Next
    In the above code, we searched the given item into an array and then shift items of the array to delete the given item from the list. If an item is not found in the list then, an error message will be printed on the console screen.
    At last, we print the updated array on the console screen.
    VB.Net Array Programs »
    
    
    
    
   
    
  
    Advertisement
    
    
    
  
  
    Advertisement