A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

比如一个数组。

6 个回复

倒序浏览
  1.    int[] values = { 1, 3, 9, 99, 97, 5, 3 };
  2.             HashSet<int> set = new HashSet<int>();
  3.             foreach (int i in values)
  4.             {
  5.                 set.Add(i);
  6.             }

  7.             foreach (int i in set)
  8.             {
  9.                 Console.WriteLine(i);
  10.             }
复制代码
HashSet用于盛放不同的数据,相同的数据只保留一份

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
  1. int[] arr = { 1,3,8,12,3,4,21};
  2. int count = 0;
  3. Dictionary<int, int> dic = new Dictionary<int, int>();
  4. //先将元素和出现的次数 放入一个集合中
  5. foreach(int item in arr)
  6. {
  7. if (dic.ContainsKey(item))
  8. {
  9. dic[item] += 1;
  10. }
  11. else {
  12. dic[item] = 1;
  13. }
  14. }
  15. //对集合遍历,如果元素只出现一次,则加1,否则忽略
  16. foreach(KeyValuePair<int,int> kv in dic)
  17. {
  18. if(kv.Value==1)
  19. {
  20. count++;
  21. }
  22. }
  23. Console.WriteLine(count);
  24. Console.ReadKey();
复制代码
重复的数就不算么?如果是这个意思,下面是我的答案,如果重复的数也算一个,那前面的同学已经给出了答案

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
下面方法也可以实现:

            int[] arr = new int[] { 1, 1, 3, 4, 3, 2 };
            List<int> list = arr.ToList();                //将数组转成list
            IEnumerable<int> result = list.Distinct();    //将list中重复项删除
            Console.WriteLine(result.Count().ToString()); //输出list中元素个数,即为数组中不同元素个数

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
  1. import java.util.Arrays;


  2. public class test11 {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 int a[] = {1,2,3,4,5,6};
  9.                 int n = 0;
  10.                 for(int i =0; i < a.length; i++) {
  11.                         int temp = a[i];
  12.                         boolean tag = true;
  13.                         for(int j = i + 1; j < a.length; j++) {
  14.                                 if(temp == a[j]) {
  15.                                         tag = false;
  16.                                 }
  17.                         }
  18.                         if(tag)
  19.                                 n++;

  20.                 }
  21.                 System.out.println(n);
  22.         }

  23. }
复制代码
纯粹是小算法!
回复 使用道具 举报
本帖最后由 _王涛 于 2013-4-3 22:53 编辑

给你举个简单例子吧:
  1. public class StringDemo {

  2. public static void main(String[] args) {

  3. int[] arr3={12,7,1,12,8,12,7,9,10,2};
  4. int count1=0;
  5. int count2=0;
  6. for(int i=0;i<arr3.length;i++)
  7. {
  8.   for(int j=0;j<arr3.length;j++)
  9.   {
  10.    if(arr3[i]==arr3[j])
  11.    {
  12.      count1++;
  13.    }
  14.   }
  15.     if(count1==1)
  16.    {
  17.     count2++;
  18.    }
  19.     count1=0;
  20. }
  21.    System.out.println(count2);
  22. }
  23. }
复制代码
回复 使用道具 举报
我写了一个数组长度可以是任意大小的(当然,数组长度小于2的排除掉了),得到不重复元素个数的程序,请指点!
import java.util.Arrays;

public class NonDuplicates {
        public static void main(String[] argus){
                System.out.println("a1:"+nondup(new int[]{}));
                System.out.println("a2:"+nondup(new int[]{1}));
                System.out.println("a3:"+nondup(new int[]{1,1}));
                System.out.println("a4:"+nondup(new int[]{1,3,2,1,5,5}));
                System.out.println("a5:"+nondup(new int[]{3,2,1,2,5,3,4,6,7,9}));
               
        }
static int nondup(int[] a){
        if(a.length<2)
                return 0;
        int count=0;
        Arrays.sort(a);
        if(a[0]==a[1])count++;
        if(a[a.length-1]==a[a.length-2])
                count++;
       
        for(int i=1;i<a.length-1;i++){
                if(a[i]==a[i-1]||a[i]==a[i+1]){
                        count++;
                }
        }
        return a.length-count;
}
}


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马