How to delete element from array? - c#

I have an string array like this
K={"book","handbook","car"}
I would like to check if any string contains other string,if it does I would like to remove it from array .In the case of array K,new array should be like this
K="{"book","car"}
for (i = 0; i < 10; i++)
{
if (keywords.Contains(keywords[i])) {
//I have no idea for this part
}
}

It might make more sense to use a List<>, a data structure designed for editing, whereas an array is a fixed structure. But, assuming you have an array, and need to end up with a modified array, you could convert:
IEnumerable<string> invalid_words = // ... list of words K cannot contain
string[] K = // .. array of strings you are given
K = K.Where(p => !invalid_words.Contains(p)).ToArray();

This might be a little "smelly" but it's better than try to modify the array when you are in an iteration.
The idea is this: save all the indexes where the word appears, and then erase those words. This is not the best solution, but it can help you with your problem. I highly recommends you to read about "Lists" because there are great on C# and it's easier use them than use arrays
K="{"book","car"};
//Never use a number, try to use the .length propertie
List<int> indexes=new List<int>();//using lists is easier than arrays
enter code here
for (i = 0; i < K.length; i++)
{
if (keywords.Contains(K[i]))
{
//You save the index where the word is
indexes.add(i);
}
}
//Here you take those indexes and create a new Array
int[] theNewArray=new int[K.length-indexes.Count];
int positionInTheNewArray=0;
for (i = 0; i < K.length; i++)
{
if(!indexes.Contains(i))
{ theNewArray[positionInTheNewArray]=K[i];
positionInTheNewArray++;
}
}
}
That fits if your array allows duplicated words and also if duplicated words are not allowed.

Related

Loop to Check if Index Has Repeated in C#

I would like to create a for loop to check if an index value has repeated, and if it has to remove it from the original and display an updated list with no repeated index values.
I have and Array (called Original) with multiple values being repeated, and then I created a Temporary Array (called TempArray) with the same exact values.
I want to check the Original and TempArray values against each other to see if there are duplicates.
I want my code to be similar to a previous one I did where I deleted an index from the middle of my array, as follows
for (int i = 0; i < Original.Length; i++)
{
TempArray[i] = Original[i];
}
Original = new int[Original.Length - 1];
//Set index to delete
int DeleteIndex = 3;
//modify size of Orignial array and copy in array without deleted element index
for (int i = 0; i < TempArray.Length; i++)
{
if (i < DeleteIndex)
{
Original[i] = TempArray[i];
}
else if (i > DeleteIndex)
{
Original[i - 1] = TempArray[i];
}
}
P.S. Original[] is an array from a text file. Sorry if my explanation is hard to understand as I am new to computer programming.
Also is there any way to do this code without LINQ, I have to create using arrays only as its all thats been taught at school?
It took some time to understand what you want :)
to check if an index value has repeated, and if it has to remove it from the original and display an updated list with no repeated index values
It is shortly called distinct values. There is a LINQ method .Distinct():
Original = Original.Distinct().ToArray();
It will remove all repeated values.
where I deleted an index from the middle of my array
You can do this easier by converting to List, applying RemoveAt and back:
var list = new List<int>(Original);
list.RemoveAt(DeleteIndex);
Original = list.ToArray();
P.S. Assuming that you say "index" about the values in your array, it is pretty confusing. Array does not store indices - index is usually understood as a key of an array.
May be this will do the trick for you. I am assuming that you need to remove duplicate from your array
Original = Original.Distinct().ToArray();
Will Returns distinct elements from a sequence by using the default equality comparer to compare values.

Binary search slower, what am I doing wrong?

EDIT: so it looks like this is normal behavior, so can anyone just recommend a faster way to do these numerous intersections?
so my problem is this. I have 8000 lists (strings in each list). For each list (ranging from size 50 to 400), I'm comparing it to every other list and performing a calculation based on the intersection number. So I'll do
list1(intersect)list1= number
list1(intersect)list2= number
list1(intersect)list888= number
And I do this for every list. Previously, I had HashList and my code was essentially this: (well, I was actually searching through properties of an object, so I
had to modify the code a bit, but it's basically this:
I have my two versions below, but if anyone knows anything faster, please let me know!
Loop through AllLists, getting each list, starting with list1, and then do this:
foreach (List list in AllLists)
{
if (list1_length < list_length) //just a check to so I'm looping through the
//smaller list
{
foreach (string word in list1)
{
if (block.generator_list.Contains(word))
{
//simple integer count
}
}
}
// a little more code, but the same, but looping through the other list if it's smaller/bigger
Then I make the lists into regular lists, and applied Sort(), which changed my code to
foreach (List list in AllLists)
{
if (list1_length < list_length) //just a check to so I'm looping through the
//smaller list
{
for (int i = 0; i < list1_length; i++)
{
var test = list.BinarySearch(list1[i]);
if (test > -1)
{
//simple integer count
}
}
}
The first version takes about 6 seconds, the other one takes more than 20 (I just stop there cuz otherwise it would take more than a minute!!!) (and this is for a smallish subset of the data)
I'm sure there's a drastic mistake somewhere, but I can't find it.
Well I have tried three distinct methods for achieving this (assuming I understood the problem correctly). Please note I have used HashSet<int> in order to more easily generate random input.
setting up:
List<HashSet<int>> allSets = new List<HashSet<int>>();
Random rand = new Random();
for(int i = 0; i < 8000; ++i) {
HashSet<int> ints = new HashSet<int>();
for(int j = 0; j < rand.Next(50, 400); ++j) {
ints.Add(rand.Next(0, 1000));
}
allSets.Add(ints);
}
the three methods I checked (code is what runs in the inner loop):
the loop:
note that you are getting duplicated results in your code (intersecting set A with set B and later intersecting set B with set A).
It won't affect your performance thanks to the list length check you are doing. But iterating this way is clearer.
for(int i = 0; i < allSets.Count; ++i) {
for(int j = i + 1; j < allSets.Count; ++j) {
}
}
first method:
used IEnumerable.Intersect() to get the intersection with the other list and checked IEnumerable.Count() to get the size of the intersection.
var intersect = allSets[i].Intersect(allSets[j]);
count = intersect.Count();
this was the slowest one averaging 177s
second method:
cloned the smaller set of the two sets I was intersecting, then used ISet.IntersectWith() and checked the resulting sets Count.
HashSet<int> intersect;
HashSet<int> intersectWith;
if(allSets[i].Count < allSets[j].Count) {
intersect = new HashSet<int>(allSets[i]);
intersectWith = allSets[j];
} else {
intersect = new HashSet<int>(allSets[j]);
intersectWith = allSets[i];
}
intersect.IntersectWith(intersectWith);
count = intersect.Count;
}
}
this one was slightly faster, averaging 154s
third method:
did something very similar to what you did iterated over the shorter set and checked ISet.Contains on the longer set.
for(int i = 0; i < allSets.Count; ++i) {
for(int j = i + 1; j < allSets.Count; ++j) {
count = 0;
if(allSets[i].Count < allSets[j].Count) {
loopingSet = allSets[i];
containsSet = allSets[j];
} else {
loopingSet = allSets[j];
containsSet = allSets[i];
}
foreach(int k in loopingSet) {
if(containsSet.Contains(k)) {
++count;
}
}
}
}
this method was by far the fastest (as expected), averaging 66s
conclusion
the method you're using is the fastest of these three. I certainly can't think of a faster single threaded way to do this. Perhaps there is a better concurrent solution.
I've found that one of the most important considerations in iterating/searching any kind of collection is to choose the collection type very carefully. To iterate through a normal collection for your purposes will not be the most optimal. Try using something like:
System.Collections.Generic.HashSet<T>
Using the Contains() method while iterating over the shorter list of two (as you mentioned you're already doing) should give close to O(1) performance, the same as key lookups in the generic Dictionary type.

"Contains" method doesn't seem to working the way it should

I'm trying to see if when a user enters some text it searches the array for any matches, and whatever doesn't match gets removed from the array;
string search = textBox1.Text;
for (int i = 0; i < staffUsers.Count; i++)
{
if (!(staffUsers[i].staff_name.Contains(search)))
{
staffUsers.Remove(staffUsers[i]);
}
}
I have some rubbish names in my array 'Rob Dob','Joe Bloggs', 'h h', 'ghg hgh', and if the search variable ended up being 'R', Joe Bloggs would get removed but 'h h' and 'ghg hgh' stay there, but there is no R involved there at all? any reason why>?!
You have to iterate backwards in order to remove from an array. Every time you remove an item, your array gets smaller. By going backwards, that fact does not matter.
string search = textBox1.Text;
for (int i = staffUsers.Count - 1; i >= 0 ; i--)
{
if (!(staffUsers[i].staff_name.Contains(search)))
{
staffUsers.Remove(staffUsers[i]);
}
}
The problem with your code is that you are removing items as you iterate over it. You remove an item, but keep iterating, even though the size of the array changes when you remove an item.
You need to reset your i value after you remove something. Alternatively, you need to use a built in to do the heavy lifting:
staffUsers.RemoveAll(i => !(i.staff_name.Contains(search)));
Uses a tiny LINQ expression to do the work. Remove all items where that predicate matches. i represents an item to apply the expression to. If that expression evaluates to true, away it goes.
Long story short, whenever you remove an item at index [i], you skip the item at index [i+1]. For example, if your array looks like:
{'Joe Bloggs', 'Rob Dobb', 'h h', 'gafddf'}; i=0
remove Joe Bloggs, which is at position 0.
{Rob Dobb', 'h h', 'gafddf'}; i=1
remove 'h h', which is at position 1
{Rob Dobb', 'gafddf'}; i=2
i is not less than yourArray.Count, so the loop stops. There is no position 2.
The quickest fix is to add i-- if you remove something from index [i]. In your case,
staffUsers.Remove(staffUsers[i]);
i--;
Hope this helps!
In each iteration of the loop, either remove an item or increment the counter, not both operations. Otherwise, you'll skip the next array element whenever you remove an array element:
string search = textBox1.Text;
for (int i = 0; i < staffUsers.Count;)
{
if (!(staffUsers[i].staff_name.Contains(search)))
{
staffUsers.Remove(staffUsers[i]);
}
else
{
i++;
}
}
The simplest way to solve the above problem is using LINQ.
Following code disentangle above problem.
string search = textBox1.Text;
staffUsers= (from user in staffUsers
where !user.Contains(search)
select user).ToArray<string>();
Note: I assumed staffUsers is array of string.
string search = textBox1.Text;
for (int i = 0; i < staffUsers.Count; i++)
{
if (!(staffUsers[i].staff_name.Contains(search)))
{
staffUsers.Remove(staffUsers[i]);
// reset the index one stepback
i--;
}
}

C# get all elements of array

Using array.ElementAt(0); we can get the element at index 0 of the array. Is there a method to get all the elements in an array?
string[] arr1 = {'abc', 'def', 'ghi'}
Library.Results["TEST_ACTUALVALUE"] = "The results are: " + arr1;
TEST_ACTUALVALUE is a column in my report.xls file. The above writes System.string[] in my excel file.
You already have all of the elements in the array...the array itself.
The simple answer is iterate over them:
foreach(var item in array)
{
// work with item here
}
Or if you'd rather deal with indexes:
for(var i = 0; i < array.Length; i++)
{
var item = array[i];
// work with item here
}
It's hard to know what you mean by "get all the elements in an array" because you already have all the elements....in the array.
If you mean concatinating a string from a string array then something like:
var concatinatedString = String.Concat(myArrayOfStrings);
There's a hundred ways to skin that cat. Largely it should depend on what you wish to do with all the elements. Heres the old school for loop method:
for(int i = 0; i < array.Length; i++)
{
something = array[i];
}
I'm not sure why you'd do this, but there is a array.All available. Try that?
Depending on what you want to get the elements for there are many methods you can look into
Get all the elements in an array
for(int x = 0; x < array.Length; x++)
{
something = array[x];
somethingElse = array.ElementAt(x);
}
Make an array a list to get all values:
List<T> myList = array.ToList<T>();
From here you can do all list methods with that array to get all values.
If you are trying to take the sum of every element use:
sum = array.Sum();
To get the max/min value in the array:
max = array.Max();
min = array.Min();
To get the average:
double average = array.Average(num => Convert.ToInt64(num));
There are many other ways to get all information from an array, here
The question is how to join a string array together. Solution:
... = "The results are: " + String.Join (", ", arr1);
And a note: you don't have to and should not use array.ElementAt(0) to get an element. Use array[0].
Going to go out on a limb here and recommend using a loop?
Why are you using ElementAt? You can just use the indexer:
var value = array[0];
In general:
var ithValue = array[i];
The Array class implements the IList, ICollection, and IEnumerable interfaces. So you can use any needed method of these interfaces.
Eventually, you can write foreach loop to traverse through array.
//IF the array type is string, you can follow below:
//else you need to convert to string 1st.
String Result = "";
For (int i=0; i<array.length; i++)
{
Result = Result + array[i];
}
// The result will consist all the array element.

How to Use Array.Exists with the Multi-dimensional string array

I have a multi-dim string array something like this:-
string[,] names = new string[2, 2] { {"Rosy",""}, {"Peter","Albert"} };
Now i want to check the existence if the second index (Albert) holding the string is non-empty in the whole array. I just to check the existence of the non-empty string value in the second index.
I was thinking of using the Array.Exists. If there is any other better way, please share.
Thanks
I don't think you can use Array.Exists here, because that only deals with the values - you're interested in the position too. I would just use a loop:
bool found = false;
for (int i = 0; i < names.GetLength(0); i++)
{
if (!string.IsNullOrEmpty(names[i, 1]))
{
found = true;
break;
}
}
Rectangular arrays are basically a bit of a pain to work with in C#. If you had a jagged array - an array of arrays - it would be easy:
bool found = jagged.Select(x => x[1])
.Any(value => !string.IsNullOrEmpty(value));

Categories

Resources