1

I have a large one dimensional array that is called ArrayHold which is populated at runtime. I run a loop to scan through the array to find out which elements need to be removed based on a few parameters. This all works great, now I am left with two arrays. The original one and a new one which contains the locations of the elements to be removed.

Original Array:

("A")("B")("C")("D")("E")("F")("G")("H") 

Second Array with index/count of elements that needs to be removed:

("0")("3")("5")("7") 

End result should be preferably not in a new array but a "ReDim" of the original array:

("B")("C")("E")("G") 

What would be the simplest way to achieve this? I could run a loop to make all the elements that need to be removed "0" or ""? Would there be an easy way of resizing and array by dropping/removing all the white space or empty elements?

Thanks so much in advance.. :)

3
  • 2
    have you considered using Linq for this? Commented Jun 2, 2012 at 11:19
  • No I haven't I don't know much about Linq. Could you elaborate a little bit and point me in the right direction. Not look for code just looking for the best way to do it. Then Ill work the code out. Commented Jun 2, 2012 at 11:42
  • Are you talking about a Linq DataSet then try something like TypedDataSet - Checking for null to remove nulls? or am i way off :) Commented Jun 2, 2012 at 12:02

1 Answer 1

1

I would suggest to use LINQ for this, your code becomes more readable and maintanable then.

Since you haven't shown how you've filtered the first array, i assume for the example that you want to remove all "A","D","F","H" (case sensitive):

Dim original = {"A", "B", "C", "D", "E", "F", "G", "H"} ' remove A,D,F,H Dim result = (From str In original Where Not {"A", "D", "F", "H"}.Contains(str)).ToArray() 

Result:

 (0) "B" String (1) "C" String (2) "E" String (3) "G" String 

End result should be preferably not in a new array but a "ReDim" of the original array: ("B")("C")("E")("G")

Of course above creates a new array, but Redim creates also a new array.

Edit: Here's an example which removes elements based on the index, assuming you have an int[] with all indices that you want to delete from the first array:

Dim deleteIndices = {0, 3, 5, 7} ' remove elements with index 0,3,5,7 Dim result = original. Where(Function(str, index) Not deleteIndices.Contains(index)). ToArray() 
Sign up to request clarification or add additional context in comments.

8 Comments

thank you for the response, Will this remove the elements based on there value or there index? because there are duplicate elements with the same value that must remain.
@user1432290: It won't "remove" elements, it'll create a new array based on the original elements without "B","C","E","G". If you would show us a complete sample with the desired result based on your rules, it would be much easier to help :) What are the rules/parameters you use to remove elements from ArrayHold?
Yes I understand that it will have to be a new array now, no problem there. In the above example if "A" was in the array more than once, would it copy all the "A"s to the new array? or only the "A" at the index i need copied across to the new array? I haven't posted code because all my code is functional.
@user1432290: I can only repeat myself, provide your rules and i can show you how you could do that with LINQ (all things are possible). It's redundant to store the elements/indexes you want to delete in another array when you can remove them in the first place with LINQ.
The rules are basically when ever it finds a pattern of hops though the array where every hop has the same value. but there will be other values in the array just not where the hop landed.. sorry if it doesn't make sense. I also don't really want to give the idea of my app away :) hope you can understand.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.