Home > Enterprise >  Comparing Names in array to themselves
Comparing Names in array to themselves

Time:03-15

I have an Array of names I sorted alphabetically. Many names repeat themselves and I track each occurrence as Popularity I have been trying to figure out how I can compare each Index and the one next to it to see if its the same name or not. Each time the same name appears I have a counter that ticks up, when it reaches a different name it checks its occurrence vs "foundNamePop" it stores the counter in a separate variable and resets. The problem is that some Arrays as input have the same name repeating at the end of the array (i.e. Lane, Lane, Lane \0) it leaves out of my IF LOOP and doesn't store it because I just have only the "nameCounter ". I just can't seem to find the solution to making sure it reads every name and store it all no matter if there are multiple names at the end or single names that are different i.e.(Lane, Dane, Bane \0).

Let me also add these .txt files can contain ~50 thousand names and I have no idea what names are in there.

        int nameCounter = 1;
        int lessPopNameSum = 0;
        // foundNamePop is the name someone inputted to search through the array and find it's occurrence. 
        // search sameLengthName list
        for (int i = 0; i < sameLengthName.Length - 1; i  )
        {
            if (sameLengthName[i] == sameLengthName[i   1])
            {
                nameCounter  ;
            }
            else if (sameLengthName[i] != sameLengthName[i   1])
            {
                if (foundNamePop < nameCounter)
                {
                    nameCounter = 1;
                }
                else
                {
                    lessPopNameSum  = nameCounter;
                    nameCounter = 1;
                }
            }
        }

CodePudding user response:

The simple solution is to add a check after the loop

if (foundNamePop >= nameCounter)
{
      lessPopNameSum  = nameCounter;
}

But it is not clear to me what you are actually computing, it looks like you are summing the duplicate names that have more duplicates than foundNamePop, but it is not clear what value this has, nor what actual meaning the result will have.

You should be able to use LINQ to get something similar with less code:

var lessPopNameSum = sameLengthName
    .GroupBy(n => n)
    .Select(group => group.Count())
    .Where(c => c >= foundNamePop)
    .Sum();

CodePudding user response:

Although I like the elegance of the other posted solution another alternative could be to use a Dictionary to store a count of each of the names.

const int FoundNamePop = 2;

var names = new string[] { "Bill", "Jane", "Jeff", "Rebecca", "Bill" };
var count = FindPopularNames(names)
    .Where(kvp => kvp.Value < FoundNamePop)
    .Sum(kvp => kvp.Value);

// With 'FoundNamePop' set to two, the below line will print '3'.
Console.WriteLine($"Count: {count}");

static IDictionary<string, int> FindPopularNames(IEnumerable<string> names)
{
    var dict = new ConcurrentDictionary<string, int>
        (StringComparer.OrdinalIgnoreCase);

    foreach (var name in names)
    {
        dict.AddOrUpdate(name, 1, (_, count) =>   count);
    }
    return dict;
}
  • Related