一个含有字符串的数组,例如:["a","b","a","c","b","d","a"],需要得到一个新数组 ["a1","b1","a2","c","b2","d","a3"] c#
时间: 2024-06-02 09:04:45 浏览: 26
你可以通过遍历原数组,并使用 Dictionary 来统计每个字符串出现的次数,然后再次遍历原数组,根据每个字符串出现的次数,构造新的字符串数组。以下是一个示例代码:
```
string[] originalArray = new string[] { "a", "b", "a", "c", "b", "d", "a" };
Dictionary<string, int> countDict = new Dictionary<string, int>();
string[] newArray = new string[originalArray.Length];
// 统计每个字符串出现的次数
for (int i = 0; i < originalArray.Length; i++)
{
string str = originalArray[i];
if (countDict.ContainsKey(str))
{
countDict[str]++;
}
else
{
countDict[str] = 1;
}
int count = countDict[str];
newArray[i] = str + count;
}
// 输出新的字符串数组
foreach (string str in newArray)
{
Console.Write(str + " ");
}
```
相关问题
c#中 一个含有字符串的数组,例如:["a","b","a","c","b","d","a"],需要得到一个新数组 ["a1","b1","a2","c","b2","d","a3"]
你可以使用以下代码来实现你的需求:
```csharp
string[] array = { "a", "b", "a", "c", "b", "d", "a" };
List<string> newArray = new List<string>();
Dictionary<string, int> countDict = new Dictionary<string, int>();
foreach (string item in array)
{
if (countDict.ContainsKey(item))
{
countDict[item]++;
newArray.Add(item + countDict[item]);
}
else
{
countDict[item] = 1;
newArray.Add(item + countDict[item]);
}
}
string[] resultArray = newArray.ToArray();
```
这段代码首先创建了一个空的`List<string>`来存储新的数组。然后,使用一个`Dictionary<string, int>`来记录每个字符串出现的次数。接下来,遍历原始数组中的每个元素,如果该元素已经在字典中存在,则将其对应的计数加1,并将带有计数的字符串添加到新数组中;如果该元素在字典中不存在,则将其添加到字典中,并将带有计数的字符串添加到新数组中。最后,将新数组转换为字符串数组并返回。
个含有字符串的数组,例如:["a","b"."a” ,b”,d”.a”1,1].需要得到一个新数组 ["a1""b1""a2""c","b2""d""a3""11""12"]
我认为输入的数组中有一些错误,应该是这样的:["a","b","a","b","d","a","1","1"],而不是["a","b"."a” ,b”,d”.a”1,1]。在这个假设下,以下是一个可以得到新数组的实现:
```python
def get_new_array(arr):
d = {}
new_arr = []
for s in arr:
if s in d:
d[s] += 1
new_arr.append(s + str(d[s]))
else:
d[s] = 1
new_arr.append(s + str(d[s]))
return new_arr
arr = ["a","b","a","b","d","a","1","1"]
new_arr = get_new_array(arr)
print(new_arr) # ["a1","b1","a2","b2","d1","a3","11","12"]
```
这个实现中,我们用一个字典 `d` 来记录每个字符串出现的次数,然后遍历输入数组,对于每个字符串,如果它已经出现过,就在它后面加上它出现的次数,否则就把它加入字典,并在后面加上 1。最后返回新数组。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)