Click here to Skip to main content
15,444,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to find a way to use a for-loop to find how many strings that are the same word in the same array-list. I need to use the for-loop because everytime it loops and finds another word that is the same I want to add it to an int variable :
the following is the code that have tried to use but it didn;t work:

What I have tried:

C#
foreach (string row in RoomType)
{
    if (row.Equals("Bedroom"))
    {
        bedrooms++;
    }
}
Posted
Updated 28-Feb-17 10:02am
v2
Comments
Patrice T 28-Feb-17 15:26pm    
Show code that set variable 'RoomType', we can see some running code.
[no name] 28-Feb-17 16:02pm    
Yeah.... "didn't work" is not at all helpful in describing your problem.

There was a "Coding Challenge" recently that did something very similar: Coding challenge: find the repeated items in a collection of elements.[^]

From Solution 11[^] :
C#
public static class HelperExtension
{
    public static IEnumerable<T> GetRepeats<T>(this IList<T> items)
        => items?.Intersect(
               items.Where(x => items.Where(y => Equals(x, y)).Count() > 1));
}
This works with a List and not an array but would not be hard to modify. The extension method will take a list of words and gives you the repeats.
 
Share this answer
 
v2
You could also try :
C#
foreach (string row in RoomType)
{
if (row.Contains("Bedroom"))
{
bedrooms++;
}
} 
 
Share this answer
 
Comments
Patrice T 28-Feb-17 16:03pm     CRLF
Just copying the code from the question is not a solution. Please delete
CHill60 28-Feb-17 16:37pm    
To be fair Ralf has used Contains and not Equals
Patrice T 28-Feb-17 17:00pm    
Oops
CHill60 28-Feb-17 17:08pm    
:-)
Ralf Meier 1-Mar-17 8:11am    
Yes ... beacause I understand that the searched String is part the Source-String and not equal to it ...
Here is another solution using Linq:
C#
var values = new string[6] { "red", "green", "blue", "red", "red", "green" };
foreach (var item in values.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => new { Value = g.Key, Count = g.Count() }))
    Debug.WriteLine($"{item.Value} x {item.Count}");
 
Share this answer
 
Quote:
I need to use the for-loop because everytime it loops and finds another word that is the same I want to add it to an int variable :

I fear you misunderstood the usage of the for-loop and that you want to use it for the wrong reason.
You need a for-loop because you need to check every element of RoomType against a reference word, not because you want to count how many times you encounter the word.

Basically, I see nothing wrong in your code, but without the code around, it is difficult to really tell.
Complete code with a sample of what is in Roomtype.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900