Advertisement
Huskymeister

Replace In Array

Feb 18th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1.         public static void replaceInArray(ref int[] arr, int find, int replace)
  2.         {
  3.             int[] newArr = getIndices(arr, find);
  4.  
  5.             for(int i = 0; i < newArr.Length - 1; i++)
  6.             {
  7.                 arr[newArr[i]] = replace;
  8.             }
  9.         }
  10.  
  11.         public static int[] getIndices(int[] arr, int find)
  12.         {
  13.             int[] newArr = new int[arr.Length - 1];
  14.             int count = 0;
  15.  
  16.             for (int i = 0; i < arr.Length; i++)
  17.             {
  18.                 if (arr[i] == find)
  19.                 {
  20.                     newArr[i] = i;
  21.                     count++;
  22.                 }
  23.             }
  24.  
  25.             int[] indexArr = new int[count];
  26.  
  27.             for(int i = 0; i < indexArr.Length; i++)
  28.             {
  29.                 indexArr[i] = newArr[i];
  30.             }
  31.  
  32.             return indexArr;
  33.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement