版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Xidian2850/article/details/89737402
package com.hzy.utils;
import org.junit.Test;
public class HzySortAlgorithm {
/*
* 算法思路:后面的元素不断地和前面已过滤完成的进行比较,
* 下标i和index不断往后移,i记录整个数组元素的下标,index记录重复元素的原位置下标,
* 为后面不同的元素覆盖
*/
//不能用hashSet算法来处理,因为HashSet默认自然数排序
public static int[] getNotRepeatSort(int[] arr) { //arr为店家id的数组
int index = 1 ;//关键
boolean isExit = false;
for(int i = 0 ; i < arr.length ; i++)
{
for (int j = 0 ; j < index ;j++ )
{
if(arr[i] == arr[j])
{
isExit = true ;
break;//黄朝阳优化
}
}
if (!isExit)
{
arr[index]= arr[i];//覆盖第index+1个位置的数
index++;
}
isExit = false ;
}
int result[] = new int[index];
for (int i = 0; i < index; i++) {
result[i] = arr[i];
}
return result;//返回过滤完成的数组
}
@Test
public void test(){
int[] arr = {2,2,3,3,5,4,2,1,3,5,1,1,2,5,8,3};
int[] r = getNotRepeatSort(arr);
for(int i =0 ;i<r.length; i++){
System.out.println(r[i]);
}
}
}