Array/String intersect and Array/Array intersect code

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Array/String intersect and Array/Array intersect code

 
1
  #1
Jul 23rd, 2006
got to thinking that i couldnt find a way to find the intersect of a string and array nor could i find a quick way to do an array / array.


i searched a bit on google and didnt really find anything that showed me something like

Array.Intersect(array a, array b)

so i wrote my own

i hope this helps someone out there. even if there is a faster way it is nice to discover how things were/are done.

array vs string intersect.
  1. public static string Intersect(string[] arrayA,string intersectObject)
  2. {
  3. int arrayALength = arrayA.Length;
  4. int count = 0,
  5. maxCount = arrayALength;
  6. string strOutput = "";
  7. //this is only going to find one intersection. thats all i really give a hoot about
  8. while(count >= 0 && count < maxCount)
  9. {
  10. if(arrayA[count] == null)
  11. {
  12. //if it finds a null value
  13. arrayA[count] = "";//insert a blank space
  14. }
  15. if(arrayA[count].Equals(intersectObject))
  16. {
  17. //if it finds an intersection
  18. strOutput = intersectObject;
  19. }
  20. count++;
  21. }
  22. //return the results
  23. if(strOutput != intersectObject)
  24. {
  25. strOutput = "false";
  26. }
  27. return strOutput;
  28. }


array vs array intersect
  1. public static string[] arrayIntersect(string[] arrayA, string[] arrayB)//returns ONLY the first found strings in common between array A and array B
  2. {
  3. //count the length of both arrays
  4. int intArrayA = arrayA.Length,
  5. intArrayB = arrayB.Length;
  6. //Find Max array size if all vallues from shortest match all the values from the biggest
  7. int intMaxOutput = 0;
  8. string[] longArray, shortArray;
  9. if (intArrayA >= intArrayB)
  10. {
  11. intMaxOutput = intArrayB;
  12. shortArray = new string[intArrayB];//declare its size
  13. shortArray = arrayB;//transfer all value
  14. longArray = new string[intArrayA];//declare its size
  15. longArray = arrayA;//transfer all values
  16. }
  17. else//intArrayB is bigger
  18. {
  19. intMaxOutput = intArrayA;
  20. shortArray = new string[intArrayA];//declare its size
  21. shortArray = arrayA;//transfer all value
  22. longArray = new string[intArrayB];//declare its size
  23. longArray = arrayB;//transfer all values
  24. }
  25. //array for storing the strings
  26. string[] strArrayOutput = new string[intMaxOutput];
  27. //If all possible values matches then the shortest array will determin the max number of matches.
  28. int shortCount = 0;//count through short array
  29. int longCount = 0;//count through long array
  30. int intOutput = 0;//starts the counter for each value that could be found
  31.  
  32. /*
  33. * !!!side not to self!!!
  34. * since we know which of the arrays is the smallest
  35. * we only need to count through the while statement as many times
  36. * as the shortest array length, testing it each time against the other array
  37. * each time a value is found it needs to be stored into the array output
  38. *
  39. */
  40. //take care of any null values in the short array
  41. while (shortCount >= 0 && shortCount < shortArray.Length)
  42. {
  43. if(shortArray[shortCount] == null)
  44. {
  45. shortArray[shortCount] = "";
  46. shortCount++;
  47. }
  48. else
  49. {
  50. shortCount++;
  51. }
  52. }
  53. //take care of any null values in the long array
  54. while (longCount >= 0 && longCount < longArray.Length)
  55. {
  56. if(longArray[longCount] == null)
  57. {
  58. longArray[longCount] = "";
  59. longCount++;
  60. }
  61. else
  62. {
  63. longCount++;
  64. }
  65. }
  66. //reset counters
  67. longCount = 0;
  68. shortCount = 0;
  69. //begin array checking
  70. while(shortCount >= 0 && shortCount < intMaxOutput)
  71. {
  72. //If longCount is at max but shortCount is not
  73. if(longCount >= (longArray.Length - 1) && shortCount != (shortArray.Length - 1))
  74. {
  75. //test to make sure these values do not match
  76. if(shortArray[shortCount] != longArray[longCount])
  77. {
  78. //we are still testing values
  79. longCount = 0;//reset the long counter
  80. shortCount++;//update the short counter
  81. }
  82. else
  83. {
  84. //something matches
  85. strArrayOutput[intOutput] = shortArray[shortCount];
  86. intOutput++;//incriment the output array
  87. longCount = 0;//reset the long counter
  88. shortCount++;//update the short counter
  89. }
  90. }
  91. if(shortCount >= (shortArray.Length -1) && longCount >= (longArray.Length -1))
  92. {
  93. //we have left the boudns of this array
  94. //do nothing
  95. shortCount++;//this will end the while statment
  96. }
  97. else
  98. {
  99. //test for match
  100. if(shortArray[shortCount] != longArray[longCount])
  101. {
  102. //if the two array strings do not match
  103. longCount++;
  104. //increase the count of the longer array
  105. }
  106. else
  107. {
  108. //if the two array strings do match
  109. strArrayOutput[intOutput] = shortArray[shortCount];//assign the value to the return array
  110. intOutput++;//incriment the output array
  111. shortCount++;//incriment the A counter
  112. longCount = 0;//reset the B counter
  113. }
  114. }
  115. }
  116. return strArrayOutput;
  117. }
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Array/String intersect and Array/Array intersect code

 
0
  #2
Jul 30th, 2006
Awesome! Thanks for submitting!
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC