在字符串数组中查找重复的条目? [英] Find duplicate entries in an array of strings?

查看:106
本文介绍了在字符串数组中查找重复的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有一个很大的字符串单元格数组.我需要在此数组中找到重复字符串的索引.也就是说,我期望的输出是在单元格字符串数组中出现两次或更多次的字符串索引数组.

I have a large cell array of strings in Matlab. I need to find the indexes of duplicate strings in this array. That is, the output I expect is an array of the indices of strings that appear two or more times in the cell array of strings.

我该怎么做?

推荐答案

您可以对数组进行排序,然后检查每个单元格是否等于以下单元格. 运行时= O(N log(N)) 我不记得有内置功能了.

You can order the array, and then check for each cell if it equals the following cell. Runtime = O(N log(N)) I don't recall a built-in function for that.

Arr = ['aa' 'bb' 'cc' 'bb'];
ArrSort = sort(Arr);// Arr = ['aa' 'bb' 'bb' 'cc']

NewArr = ArrSort(1);
newInd = 1;
for i=2:length(ArrSort)
    if NewArr(newInd) ~= ArrSort(i)
       newInd = newInd + 1;
       NewArr(newInd) = ArrSort(i)
    end
end

这篇关于在字符串数组中查找重复的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆