daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Jan 20th, 2018 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Solution
  5. {
  6.     public static int[] FindDuplicates(int[] arr1, int[] arr2)// [1,2,3,5,6,7], [3, 6,7,8 20]
  7.     {
  8.       if(arr1 == null || arr1.Length == 0 || arr2 == null || arr2.Length == 0) // false
  9.       {
  10.         return new int[0];
  11.       }
  12.      
  13.       var length1 = arr1.Length; // 6
  14.       var length2 = arr2.Length; // 5
  15.      
  16.       var index1 = 0;
  17.       var index2 = 0;
  18.      
  19.       var duplicate = new List<int>();
  20.      
  21.       while(index1 < length1 && index2 < length2)  // false
  22.       {
  23.         var current1 = arr1[index1]; // 1, 2, 3, 5, 6, 7
  24.         var current2 = arr2[index2]; // 3, 6, 7
  25.        
  26.         if(current1 == current2) //false, true
  27.         {
  28.           duplicate.Add(current1); // 3, 6, 7
  29.           index1++;  // 3, 5, 6
  30.           index2++; // 1, 2, 3
  31.         }
  32.         else if(current1 < current2)// true
  33.         {
  34.           index1++; // 1, 2, 4
  35.         }
  36.         else
  37.         {
  38.           index2++;
  39.         }
  40.       }
  41.      
  42.       return duplicate.ToArray();
  43.     }
  44.  
  45.     static void Main(string[] args)
  46.     {
  47.         var duplicated = FindDuplicates(new int[]{1, 2,3, 5, 6, 7}, new int[]{3, 6, 7, 8, 20});
  48.         foreach(var item in duplicated)
  49.         {
  50.           Console.WriteLine(item);
  51.         }
  52.     }
  53. }
  54.  
  55. /*
  56. case m almost same as n
  57.  
  58. m > n
  59. first array first
  60.  
  61. [1, 2, 3, 5, 6, 7]
  62.  
  63. index1 = 0
  64. [3, 6, 7, 8, 20, 8]
  65.  
  66. index2 = 0;
  67.  
  68. O(m + n)
  69.  
  70. // iterate
  71.  
  72. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top