Advertisement
Pazzobg

Arrays-Methods-Exercises-Array-Distinct-Algorhytm

Jun 9th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. namespace _02.ManipulateArray
  2. {
  3.     using System;
  4.  
  5.     public class ManipulateArray
  6.     {
  7.         public static void Main()
  8.         {
  9.             string[] inputArray = Console.ReadLine().Split(' ');
  10.  
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             for (int i = 0; i < n; i++)
  14.             {
  15.                 string[] command = Console.ReadLine().Split(' ');
  16.  
  17.                 if (command[0] == "Reverse")
  18.                 {
  19.                     ReverseArray(inputArray);
  20.                 }
  21.                 else if (command[0] == "Distinct")
  22.                 {
  23.                     inputArray = GetDistinctElements(inputArray);
  24.                 }
  25.                 else if (command[0] == "Replace")
  26.                 {
  27.                     ReplaceCertainElement(inputArray, command);
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine(string.Join(", ", inputArray));
  32.         }
  33.  
  34.         public static void ReverseArray(string[] inputArray)
  35.         {
  36.             for (int i = 0; i < inputArray.Length / 2; i++)
  37.             {
  38.                 string tempValue = inputArray[i];
  39.  
  40.                 inputArray[i] = inputArray[inputArray.Length - 1 - i];
  41.                 inputArray[inputArray.Length - 1 - i] = tempValue;
  42.             }
  43.         }
  44.  
  45.         public static string[] GetDistinctElements(string[] inputArray)
  46.         {
  47.             int size = inputArray.Length;
  48.  
  49.             for (int i = 0; i < inputArray.Length; i++)
  50.             {
  51.                 if (inputArray[i] != null)
  52.                 {
  53.                     for (int j = i + 1; j < inputArray.Length; j++)
  54.                     {
  55.                         if (inputArray[i] == inputArray[j] && inputArray[i] != null)
  56.                         {
  57.                             inputArray[j] = null;
  58.                             size--;
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.  
  64.             string[] newArray = new string[size];
  65.             int counter = 0;
  66.  
  67.             for (int i = 0; i < inputArray.Length; i++)
  68.             {
  69.                 if (inputArray[i] != null)
  70.                 {
  71.                     newArray[counter] = inputArray[i];
  72.                     counter++;
  73.                 }
  74.             }
  75.  
  76.             return newArray;
  77.         }
  78.  
  79.         public static void ReplaceCertainElement(string[] inputArray, string[] command)
  80.         {
  81.             int index = int.Parse(command[1]);
  82.             string newValue = command[2];
  83.  
  84.             inputArray[index] = newValue;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement