给定一个数组和一个数字“x”,编写一个函数从给定数组中删除“x”。我们假设阵列有两个功能:容量和大小。所以当我们移除一件物品时,容量不会改变,只会改变大小。 例子:
null
Input: arr[] = {3, 1, 2, 5, 90}, x = 2, size = 5, capacity = 5Output: arr[] = {3, 1, 5, 90, _}, size = 4, capacity = 5Input: arr[] = {3, 1, 2, _, _}, x = 2, size = 3, capacity = 5Output: arr[] = {3, 1, _, _, _}, size = 2, capacity = 5
方法1(先搜索,然后删除) 我们首先在数组中搜索“x”,然后将位于x右侧的元素返回到后面的一个位置。下面是这个简单方法的实现。
C++
// C++ program to remove a given element from an array #include<bits/stdc++.h> using namespace std; // This function removes an element x from arr[] and // returns new size after removal (size is reduced only // when x is present in arr[] int deleteElement( int arr[], int n, int x) { // Search x in array int i; for (i=0; i<n; i++) if (arr[i] == x) break ; // If x found in array if (i < n) { // reduce size of array and move all // elements on space ahead n = n - 1; for ( int j=i; j<n; j++) arr[j] = arr[j+1]; } return n; } /* Driver program to test above function */ int main() { int arr[] = {11, 15, 6, 8, 9, 10}; int n = sizeof (arr)/ sizeof (arr[0]); int x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); cout << "Modified array is " ; for ( int i=0; i<n; i++) cout << arr[i] << " " ; return 0; } |
JAVA
// Java program to remove a given element from an array import java.io.*; class Deletion { // This function removes an element x from arr[] and // returns new size after removal (size is reduced only // when x is present in arr[] static int deleteElement( int arr[], int n, int x) { // Search x in array int i; for (i= 0 ; i<n; i++) if (arr[i] == x) break ; // If x found in array if (i < n) { // reduce size of array and move all // elements on space ahead n = n - 1 ; for ( int j=i; j<n; j++) arr[j] = arr[j+ 1 ]; } return n; } // Driver program to test above function public static void main(String[] args) { int arr[] = { 11 , 15 , 6 , 8 , 9 , 10 }; int n = arr.length; int x = 6 ; // Delete x from arr[] n = deleteElement(arr, n, x); System.out.println( "Modified array is" ); for ( int i = 0 ; i < n; i++) System.out.print(arr[i]+ " " ); } } /*This code is contributed by Devesh Agrawal*/ |
Python3
# Python 3 program to remove a given # element from an array # This function removes an element x # from arr[] and returns new size after # removal (size is reduced only when x # is present in arr[] def deleteElement(arr, n, x): # Search x in array for i in range (n): if (arr[i] = = x): break # If x found in array if (i < n): # reduce size of array and move # all elements on space ahead n = n - 1 ; for j in range (i, n, 1 ): arr[j] = arr[j + 1 ] return n # Driver Code if __name__ = = '__main__' : arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] n = len (arr) x = 6 # Delete x from arr[] n = deleteElement(arr, n, x) print ( "Modified array is" ) for i in range (n): print (arr[i], end = " " ) # This code is contributed by # Shashank_Sharma |
C#
// C# program to remove a given element from // an array using System; class GFG { // This function removes an element x // from arr[] and returns new size // after removal (size is reduced only // when x is present in arr[] static int deleteElement( int []arr, int n, int x) { // Search x in array int i; for (i = 0; i < n; i++) if (arr[i] == x) break ; // If x found in array if (i < n) { // reduce size of array and // move all elements on // space ahead n = n - 1; for ( int j = i; j < n; j++) arr[j] = arr[j+1]; } return n; } // Driver program to test above function public static void Main() { int []arr = {11, 15, 6, 8, 9, 10}; int n = arr.Length; int x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); Console.WriteLine( "Modified array is" ); for ( int i = 0; i < n; i++) Console.Write(arr[i]+ " " ); } } // This code is contributed by nitin mittal. |
Javascript
<script> // Javascript program to remove a // given element from an array // This function removes an // element x from arr[] and // returns new size after removal // (size is reduced only // when x is present in arr[] function deleteElement( arr, n, x) { // Search x in array let i; for (i=0; i<n; i++) if (arr[i] == x) break ; // If x found in array if (i < n) { // reduce size of array and move all // elements on space ahead n = n - 1; for (let j=i; j<n; j++) arr[j] = arr[j+1]; } return n; } // driver code let arr = [11, 15, 6, 8, 9, 10]; let n = arr.length; let x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); document.write( "Modified array is </br>" ); for (let i=0; i<n; i++) document.write(arr[i] + " " ); </script> |
输出:
Modified array is11 15 8 9 10
方法2(搜索时移动元素) 此方法假定元素始终存在于数组中。这个想法是从最右边的元素开始,在搜索“x”的同时不断移动元素。下面是C++和java实现的这种方法。请注意,当数组中不存在“x”时,这种方法可能会产生意外的结果。
C++
// C++ program to remove a given element from an array #include<iostream> using namespace std; // This function removes an element x from arr[] and // returns new size after removal. // Returned size is n-1 when element is present. // Otherwise 0 is returned to indicate failure. int deleteElement( int arr[], int n, int x) { // If x is last element, nothing to do if (arr[n-1] == x) return (n-1); // Start from rightmost element and keep moving // elements one position ahead. int prev = arr[n-1], i; for (i=n-2; i>=0 && arr[i]!=x; i--) { int curr = arr[i]; arr[i] = prev; prev = curr; } // If element was not found if (i < 0) return 0; // Else move the next element in place of x arr[i] = prev; return (n-1); } /* Driver program to test above function */ int main() { int arr[] = {11, 15, 6, 8, 9, 10}; int n = sizeof (arr)/ sizeof (arr[0]); int x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); cout << "Modified array is " ; for ( int i=0; i<n; i++) cout << arr[i] << " " ; return 0; } |
JAVA
// Java program to remove a given element from an array import java.io.*; class Deletion { // This function removes an element x from arr[] and // returns new size after removal. // Returned size is n-1 when element is present. // Otherwise 0 is returned to indicate failure. static int deleteElement( int arr[], int n, int x) { // If x is last element, nothing to do if (arr[n- 1 ] == x) return (n- 1 ); // Start from rightmost element and keep moving // elements one position ahead. int prev = arr[n- 1 ], i; for (i=n- 2 ; i>= 0 && arr[i]!=x; i--) { int curr = arr[i]; arr[i] = prev; prev = curr; } // If element was not found if (i < 0 ) return 0 ; // Else move the next element in place of x arr[i] = prev; return (n- 1 ); } // Driver program to test above function public static void main(String[] args) { int arr[] = { 11 , 15 , 6 , 8 , 9 , 10 }; int n = arr.length; int x = 6 ; // Delete x from arr[] n = deleteElement(arr, n, x); System.out.println( "Modified array is" ); for ( int i = 0 ; i < n; i++) System.out.print(arr[i]+ " " ); } } /*This code is contributed by Devesh Agrawal*/ |
Python3
# python program to remove a given element from an array # This function removes an element x from arr[] and # returns new size after removal. # Returned size is n-1 when element is present. # Otherwise 0 is returned to indicate failure. def deleteElement(arr,n,x): # If x is last element, nothing to do if arr[n - 1 ] = = x: return n - 1 # Start from rightmost element and keep moving # elements one position ahead. prev = arr[n - 1 ] for i in range (n - 2 , 1 , - 1 ): if arr[i]! = x: curr = arr[i] arr[i] = prev prev = curr # If element was not found if i< 0 : return 0 # Else move the next element in place of x arr[i] = prev return n - 1 # Driver code arr = [ 11 , 15 , 6 , 8 , 9 , 10 ] n = len (arr) x = 6 n = deleteElement(arr,n,x) print ( "Modified array is" ) for i in range (n): print (arr[i],end = " " ) # This code is contributed by Shrikant13 |
C#
// C# program to remove a given // element from an array using System; class GFG { // This function removes an // element x from arr[] and // returns new size after // removal. Returned size is // n-1 when element is present. // Otherwise 0 is returned to // indicate failure. static int deleteElement( int []arr, int n, int x) { // If x is last element, // nothing to do if (arr[n - 1] == x) return (n - 1); // Start from rightmost // element and keep moving // elements one position ahead. int prev = arr[n - 1], i; for (i = n - 2; i >= 0 && arr[i] != x; i--) { int curr = arr[i]; arr[i] = prev; prev = curr; } // If element was // not found if (i < 0) return 0; // Else move the next // element in place of x arr[i] = prev; return (n - 1); } // Driver Code public static void Main() { int []arr = {11, 15, 6, 8, 9, 10}; int n = arr.Length; int x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); Console.WriteLine( "Modified array is" ); for ( int i = 0; i < n; i++) Console.Write(arr[i]+ " " ); } } // This code is contributed by anuj_67. |
Javascript
<script> // Java Script program to remove a given element from an array // This function removes an element x from arr[] and // returns new size after removal. // Returned size is n-1 when element is present. // Otherwise 0 is returned to indicate failure. function deleteElement(arr,n,x) { // If x is last element, nothing to do if (arr[n-1] == x) return (n-1); // Start from rightmost element and keep moving // elements one position ahead. let prev = arr[n-1], i; for (i=n-2; i>=0 && arr[i]!=x; i--) { let curr = arr[i]; arr[i] = prev; prev = curr; } // If element was not found if (i < 0) return 0; // Else move the next element in place of x arr[i] = prev; return (n-1); } // Driver program to test above function let arr = [11, 15, 6, 8, 9, 10]; let n = arr.length; let x = 6; // Delete x from arr[] n = deleteElement(arr, n, x); document.write( "Modified array is<br>" ); for (let i = 0; i < n; i++) document.write(arr[i]+ " " ); // This code is contributed by sravan kumar Gottumukkala </script> |
输出:
Modified array is11 15 8 9 10
从数组中删除一个元素需要O(n)个时间,即使我们得到了要删除的元素的索引。排序数组的时间复杂度也保持O(n)。 在链表中,如果我们知道要删除的节点的前一个节点的指针,我们可以在O(1)时间内完成删除。
本文由 希曼舒 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END