计算数组中唯一字符串的数量

Counting number of unique strings in array

本文关键字:字符串 唯一 数组 计算      更新时间:2023-10-16

我一直在尝试制作一个简单的程序来计算数组中唯一字符串的数量,但我仍然想不出如果字符串重复两次以上该怎么办的解决方案。

下面是一个代码示例,其中"Tommy"在数组中存在 3 次。所以当我计算不唯一的数量时,它应该是 3,只有 2 个名字是唯一的。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" },     repeatedArray[5];
    int instance = 0, notUnique = 0, repeated = 0;
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            if (stringArray[i] == stringArray[j] && stringArray[i] != repeatedArray[repeated])
            {
                instance++;
            }
        }
        if (instance == 1)
        {
            notUnique += 2;
        }
        else if (instance >= 2)
        {
            notUnique += instance + 1;
            repeatedArray[repeated] = stringArray[i];
            repeated++;
        }
        instance = 0;
    }
    cout << "Number of non-unique strings in array is :" << notUnique << endl;
}

编辑:我不得不读两遍才能理解你想算什么。值得庆幸的是,我想到的方法可以适应这一点,所以:

一个简单的方法是记住你以易于访问的方式找到字符串的频率。我在想std::map

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
  string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" };
  std::map<string, int> count_find;
  int non_unique_count = 0, unique_count = 0;
  for(auto &s : stringArray) {
    // Note: This uses that std::map::operator[] zero-initializes when it is
    //       called for a new key, so crucially, we start with 0 (not uninitialized)
    int count = ++count_find[s];
    if(count == 1) {         // when a string is found the first time
      ++unique_count;        // tentatively mark it as unique
    } else if(count == 2) {  // when it is found the second time
      --unique_count;        // unmark as unique
      non_unique_count += 2; // and chalk both found strings up as non-unique
    } else {                 // after that, 
      ++non_unique_count;    // just chalk up as non-unique.
    }
  }
  std::cout << unique_count << ", " << non_unique_count << std::endl;
}

或者,您可以在循环通过后从unique_count计算non_unique_count,如

for(auto &s : stringArray) {
  int count = ++count_find[s];
  if(count == 1) {         // when a string is found the first time
    ++unique_count;        // tentatively mark it as unique
  } else if(count == 2) {  // when it is found the second time
    --unique_count;        // unmark as unique
  }
}
int non_unique_count = 5 - unique_count;

您想计算数组中唯一/非唯一字符串的数量,因此您只需要知道一个字符串是否唯一。

#include <iostream> #include <string> using namespace std;
 int main() {
  string stringArray[5]= {
    "Tommy", "Sammy", "Tommy", "Wally", "Tommy"
  }
  ;
  bool repeate[5];//mark whether the string is unique or not
  for(size_t i=0;
  i<5;
  ++i) repeate[i]=false;
  for (int i=0;
  i < 5;
  i++) {
    for (int j=i + 1;
    j < 5;
    j++) {
      if (stringArray[i]==stringArray[j]) {
        repeate[i]=true;
        repeate[j]=true;
      }
    }
  }
  int notUnique=0;
  for(int i=0;
  i<5;
  ++i) if(repeate[i]) ++notUnique;
  cout <<"Number of non-unique strings in array is :" << notUnique << endl;
}

类程序{

    static void Main(string[] args)
    {
            String[] input = {"abcd", "acbd", "adcb", "cdba", "bcda", "badc","sstvz","zvsst","wxvsl"};
            foreach (var item in input)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(checkMatchedCombinationOfStrings(input));
            Console.ReadKey();
    }
    
   static string sortString(string str)
    {
        string sortedString = string.Empty;
        int[] indexArray = new int[str.Length];
        for (int i = 0; i < str.Length; i++)
        {
        indexArray[i] = str[i];
        if (i > 0)
        {
            for (int j = 0; j <= i; j++)
            {
            if (indexArray[j] > indexArray[i])
            {
                indexArray[i] = indexArray[i] + indexArray[j];
                indexArray[j] = indexArray[i] - indexArray[j];
                indexArray[i] = indexArray[i] - indexArray[j];
            }
            }
        }
        }
        for (int i = 0; i < indexArray.Length; i++)
        {
        sortedString += ((char)indexArray[i]).ToString();
        }
        return sortedString;
    }
    static int checkMatchedCombinationOfStrings(String[] input)
    {
        HashSet<String> set = new HashSet<String>();
        for (int i = 0; i < input.Length; i++)
        {
            var charArraySet = sortString(input[i]);
            if (!set.Contains(charArraySet))
            {
                set.Add(charArraySet);
            }
        }
        return set.Count;
    }
}