标签:style class blog code java http
主要思想:
假设数字里的值都为正
循环判断数组 如果与前面的数字相同则变为-1
然后记录-1的个数算出重复值
然后重新new一个减去重复值长度的新数组
和原数组判断 不为-1的全部复制进来即可
代码如下:
1 package Del_Same_Num; 2 3 public class Del_Same_Num { 4 5 static int count=0; 6 7 //计算重复值 8 public static int count_same_number(int[] a) 9 { 10 for(int i=0;i<a.length;i++) 11 { 12 for(int j=i+1;j<a.length;j++) 13 { 14 if(a[i]==a[j]&&a[i]!=-1) 15 { 16 System.out.print("i="+i+":"+"j="+j+‘\t‘); 17 a[j]=-1; 18 count++; 19 System.out.println("有重复值是"+a[i]); 20 } 21 } 22 } 23 24 25 System.out.println("一共有"+count+"个重复值"); 26 return count; 27 28 } 29 30 public static int[] change(int src[]) 31 { 32 int length=src.length-count_same_number(src); 33 int[] target=new int[length]; 34 35 36 int index=0; 37 for(int i=0;i<src.length;i++) 38 { 39 if(src[i]!=-1) 40 { 41 target[index]=src[i]; 42 index++; 43 } 44 } 45 46 47 display(target); 48 return target; 49 } 50 51 //显示 52 public static void display(int[] a) 53 { 54 for(int i=0;i<a.length;i++) 55 { 56 System.out.print(a[i]+" "); 57 } 58 System.out.println(); 59 } 60 public static void main(String[] args) { 61 int a[]={1,1,1,2,2,3,4,4,5,5,6,7,19,20,21,21}; 62 display(a); 63 change(a); 64 } 65 66 }
计算一个数组里的重复值并且删去(java),布布扣,bubuko.com
标签:style class blog code java http
原文地址:http://www.cnblogs.com/sweetculiji/p/3784693.html