JAVA去掉一个已排序数组的重复数字

来源:互联网 发布:淘宝卖手办的店 编辑:程序博客网 时间:2024/05/18 04:52
原文地址:http://www.java2000.net/p11764

论坛讨论地址:http://topic.csdn.net/u/20081109/14/EB2F41BF-52C3-4F9A-A7AD-F590A83887CC.html

我的一个方案,不过肯定有提升速度的地方
  1. import java.util.Arrays;

  2. /**
  3.  * JAVA去掉一个已经排好序的数组的重复数字,尽量快.
  4.  * 
  5.  * @author 老紫竹 JAVA世纪网(java2000.net)
  6.  * 
  7.  */
  8. public class Test {

  9.   public static void main(String args[]) {
  10.     int[] arr = { 1223344555667899101111,
  11.         11121213141415 }; // 预设数据数组
  12.     int index = 1// 保存最后一个不重复的位置
  13.     int last = arr[0];
  14.     for (int i = 1; i < arr.length; i++) {
  15.       if (arr[i] != last) {
  16.         arr[index] = arr[i];
  17.         last = arr[index];
  18.         index++;
  19.       }
  20.     }
  21.     int[] rtn = new int[index];
  22.     System.arraycopy(arr, 0, rtn, 0, index);
  23.     System.out.println(Arrays.toString(rtn));
  24.   }
  25. }

特别是步长的部分。