# [面试题]去掉一个已经排好序的数组的重复数字,速度尽量快

...全文
11081 3 收藏 348

348 条回复

JavaScript_R 2012-10-19
int[] oNums ={1,2,2,3,3,3,4,4,4,4,5,5,5,5,5};
final int LENGTH = oNums.length;
int[] nums2 = new int[LENGTH];
int l=0;
int n=0;
String str="";
for(int i:oNums){
if(n<i){
n=i;
nums2[l]=n;
l++;
}
}

yjflinchong 2012-10-16

aleijie 2012-07-24
public static void main(String[] args) {
int[] oNums ={1,2,2,3,3,3,4,4,4,4,5,5,5,5,6};
int ol = oNums.length;
int[] o = new int[ol];
int lei = 0;
o[lei] = oNums[lei];
for(int i = 0;i<(ol-1);i++){
if(oNums[i]<oNums[i+1]){
lei++;
o[lei] = oNums[i+1];
}
}
for(int i = 0 ;i<o.length ; i++){
System.out.println(o[i]);
}
}

humyna 2012-05-17
[Quote=引用 10 楼 的回复:]

[/Quote]

+1

xuxinqiujiao123 2012-04-21
[Quote=引用 87 楼 的回复:]

public class Test
{
public static void ……
[/Quote]

xutao650 2012-03-09
public class Test2{
public static void main(String[] args){
int nums[] = {1,2,2,3,3,4,4,6,6,6,6,7,8,9};
int samecount=1;
int count = 0;
int k=0;
int j=0;
while(j<nums.length){
if(j!=nums.length-1){
for(int i=j;i<nums.length-1;i++){
if(nums[j] == nums[i+1]){
samecount++;
}
}
nums[j+1] = nums[j+samecount];
System.out.print(nums[j]+" ");
j=j+samecount;
samecount = 1;
count++;
}else{
System.out.print(nums[j]);
j++;
}
}
}
}

public static void distinctInt(int[] a){
System.out.print(a[0]+ " ");
if(a.length>1){
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
continue;
}else{
System.out.print(a[i]+ " ");
}
}
}
}

public static void distinctInt(int[] a){
System.out.print(a[0]+ " ");
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1]){
continue;
}else{
System.out.print(a[i]+ " ");
}
}
}

niceplay 2011-11-28

int array={2,23,423,4,2,3,4,23,4,2,34,2,3,4,23,4,23,4,234,2};
TreeMap<Integer,Integer> ts=new TreeMap<Integer,Integer>();
int length=array.length;
for(int i=0;i<length;i++)
ts.put(Integer.valueOf(array[i]),Integer,valueOf(array[i]));

pepsl6686022 2011-11-22

haotainan 2011-11-08
public class Sample
{
public static void main(String[] args)
{
int[] array = {1,2,2,3, 3, 5, 6, 7, 8, 8, 8, 10, 12, 13, 13, 15, 20};
int[] counnt=new int[array.length];

int i = 0;

while(i<array.length-1)
{
if(array[i]!=array[i+1]){

System.out.println(array[i+1]);
}
i++;
}
System.out.println(array[0]);
}
}

wang_nai_jun 2011-11-03

（1）代码最简单，内存最节省，方法定义准确。
（2）和实现语言无关。也就是不要用语言相关的类库。

zly880818 2011-10-12
#include<stdio.h>
#include<malloc.h>
#include<memory.h>

int eliminate_repeated(int array[],int ** new_array,int count)
{
int i = 0;
int j = 0;

if(NULL == array || NULL == new_array)
{
return -1;
}

for(i=0;i<count;i++)
{
if(i == 0)
{
*(*new_array+j) = array[i];
j++;
}
else
{
if(array[i] != array[i-1])
{
*(*new_array+j) = array[i];
j++;
}
}
}
return 0;
}

void main()
{
int old_array[8] = {0,0,1,2,2,3,3,4};
int *new_array = NULL;
int char_count = 1;
int i = 0;
int j=0;
int pre_char = 0;
int ret = 0;

for(i=0;i<8;i++)
{
if(old_array[i] != pre_char)
{
char_count ++;
}
pre_char = old_array[i];
}

new_array = (int *)malloc(sizeof(int)*char_count);
memset(new_array,0,sizeof(int)*char_count);

ret = eliminate_repeated(old_array,&new_array,8);

if(-1 == ret)
{
return;
}

for(i=0;i<char_count;i++)
{
printf("new_array[%d]=%d\n",i,new_array[i]);
}

}

package snt.wyb.test;

public class TestString {

/**
* JAVA去掉一个已经排好序的数组的重复数字，速度尽量快
* 注意: 已经排序好的
*/
public static void main(String[] args) {
int[] array = {1,1,2,2,3,3,3,4,4,5,9,9,9,9,12,12,12,12,12,15};
boolean[] newArr = new boolean[array[array.length-1]+1];
for(int i=0;i<array.length;i++){
newArr[array[i]] = true;
}
for(int i=0;i<newArr.length;i++){
if(newArr[i]){
System.out.print(i+"  ");
}
}

}

}

wan423016 2011-05-01
public class test7 {

public static void main(String[] args)
{
int[] a = {1, 2, 2, 3, 4, 5, 5, 5, 7, 7, 8, 9, 9, 23, 45, 75, 75, 78, 80, 99, 99, 199};

int [] b=new int[a.length];
int s=1,t=1;
int i=0;
b[0]=a[0];
while(i<a.length&&t<a.length)
{
if(a[i]!=a[t])
{
b[s]=a[t];
s++;
i=t;
}
t++;
}
for(int j=0;j<b.length;j++){
if(b[j]!=0)
System.out.print(b[j]+" ");
}
}
}

Java SE

6.2w+

Java 2 Standard Edition

2008-11-09 02:20