- 爱易网页
-
C#教程
- 数组中内容按重复次数排序,并去除重复数,该怎么解决
日期:2014-05-19 浏览次数:20814 次
数组中内容按重复次数排序,并去除重复数
数组如[a,b,b,d,e,e,f,f,f,g,e,e,f,g,g](数组仅为示例)
可看出a出现1次,b出现2此,d出现1次,e出现4此,f出现4次,g出现3次
我希望最终出现这样的排序结果:[e,f,g,b,a,d],如有可能可同时出现这样的数组
[4,4,3,2,1,1]。或者可出现类似这样的一个数组[e$4,f$4,g$3,b$2,a$1,d$1](中间添加一个$)
要求能够效率尽可能高。
------解决方案--------------------
我用java写了一段,测试通过,你可以参考一下,如果你用c#的话可能要在一些地方修改一下
public static void main(String[] args) {
Hashtable <String, Integer> ht = new Hashtable <String, Integer> ();
String[] harr={ "a ", "b ", "b ", "d ", "e ", "e ", "f ", "f ", "f ", "g ", "e ", "e ", "f ", "g ", "g "};
int fre; /*统计词频*/
/*填充hash表,重复出现的单词词频累加*/
for(int i=0;i <harr.length;i++){
//System.out.println(arr[i]);
if(!ht.containsKey(harr[i])){
ht.put(harr[i], 1);
}
else{
fre=(Integer)(ht.get(harr[i]));
fre+=1;
ht.remove(harr[i]);
ht.put(harr[i], fre);
}
}
/*调用getSortedHashtableByValue方法对词频排序*/
Map.Entry[] set = getSortedHashtableByValue(ht);
for (int i = 0; i < set.length; i++) {
/*以单词$词频的形式打印结果*/
System.out.println(set[i].getKey().toString()+ "$ "+set[i].getValue().toString()+ "\n ");
}
}
/*排序方法*/
@SuppressWarnings( "unchecked ")
public static Map.Entry[] getSortedHashtableByValue(Hashtable h) {
Set set = h.entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set.size()]);
Arrays.sort(entries, new Comparator() {
public int compare(final Object arg0, final Object arg1) {
final int key1 = Integer.parseInt(((Map.Entry) arg0).getValue()
.toString());
final int key2 = Integer.parseInt(((Map.Entry) arg1).getValue()
.toString());
return ((Comparable) key2).compareTo(key1);
}
});
return entries;
}
------解决方案--------------------
用我最熟悉的方法写了一个,测试通过,效率未知
List <string> str;
List <int> num;
private void button2_Click(object sender, EventArgs e)
{
//string[] arr = { "a ", "b ", "b ", "d ", "e ", "e ", "f ", "f ", "f ", "g ", "e ", "e ", "f ", "g ", "g " };
//string[] arr = { "4 ", "4 ", "3 ", "2 ", "1 ", "1 " };
string[] arr = { "e$4 ", "f$4 ", "g$3 ", "b$2 ", "a$1 ", "d$1 ", "g$3 ", "b$2 ", "g$3 " };
List <string> list = new List <string> (arr);
list.Sort();
StringBuilder sb = new StringBuilder();
foreach (string s in list)
{
sb.Append(s);
sb.Append( ", ");
}
string temp = sb.ToString();
str = new List <string> ();
num = new List <int> ();
temp = Regex.Replace(temp, @ "([^,]+,)\1* ", new MatchEvaluator(regReplace));
string[] strResult = str.ToArray();
int[] numResult = num.ToArray();
Array.Sort(numResult, strResult);
Array.Reverse(numResult);
Array.Reverse(strResult);