java 去除重复项
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; class Demo20
{
public static void main(String[] args)
{
//int [] arr={1,2,3,3,4,4,4,4};
int [] arr={4,2,3,3,4,4,4,4};
//arr=delArr(arr);
arr=delArrByHash(arr);
//test(arr);
System.out.println(Arrays.toString(arr));
System.out.println("这个数组的长度是:"+arr.length);
System.out.println("这个是测试去除重复项!");
}
//数组去除重复项
public static int [] delArr(int [] arr)
{
if(arr.length<=1)
return arr;
int index=-1;
//先排序
for(int i=0;i<arr.length-1;i++)
{
index=-1;
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]>arr[j])
{
index=j;
}
} if(index!=-1)
{
arr[i]=arr[i]^arr[index];
arr[index]=arr[i]^arr[index];
arr[i]=arr[i]^arr[index];
}
}
index=0;
for(int i=0;i<arr.length-1;i++)
{
if(arr[i]==arr[i+1])
index++; }
int[] arrOne=new int[arr.length-index]; index=0;
for(int i=0;i<arr.length-1;i++)
{
if(arr[i]!=arr[i+1])
{
arrOne[index]=arr[i];
index++;
}
}
arrOne[index]=arr[arr.length-1];//对最后一个数赋值 return arrOne;
} // hashSet.toArray(new int[0]); 这个查了好久,里面好像是String类型的
public static int[] delArrByHash(int [] arr)
{ Set<Integer> hashSet=new HashSet<Integer>();
for(int i=0;i<arr.length;i++)
{
hashSet.add(arr[i]);
}
int[] arrOne=new int[hashSet.size()]; Integer [] arrTow= hashSet.toArray(new Integer[0]); for(int i=0;i<arrTow.length;i++)
{
arrOne[i]=arrTow[i].intValue();
}
return arrOne;
} }
java 去除重复项的更多相关文章
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- C#- 泛型去除重复项
今天被这个问题纠结了好一会.如何去除重复项,我遇到的问题是,在判断是否重复的条件是有两个,一个信息来源,一个是信息标题. 最后使用了哈希后很好的解决,感觉挺高效的.代码贴下,做一个备忘 //防止群发, ...
- Excel2003 去除重复项
利用 数据透视表 间接 获得 非重复项 1] 选中要去除重复项 的列 数据 2] 3]将选中列移动到 左侧 即可 4] 或者导入到Access中,用sql 语句中的 distinct SELECT D ...
- txt文本怎么去除重复项
txt文本怎么去除重复项?做网络推广的朋友经常会遇到这样的问题,txt文本文件里面有许多人名或者电话号码用来发送邮件或者短信,通常有许多是重复的,下面我来介绍两个方法来去除重复项,以人名为范本讲解. ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 为Array对象添加一个去除重复项的方法
输入例子 [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq() 输出例子 [false, true, unde ...
- 对Java数组中去除重复项程序分析
我作为一个Java菜鸟,只会用简单的办法来处理这个问题.如果有大神看到,请略过,感激不尽! 所以首先先分析这道题目:数组中重复的数据进行删除,并且要让数组里的数据按原来的顺序排列,中间不能留空. 既然 ...
- LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- ajaxFileUpload js判断类型
function ajaxFileUpload() { var File_box = document.getElementById('V_file'); var extend = Fil ...
- ASP.NET批量下载服务器端指定目录文件
//遍历服务器指定文件夹下的所有文件 string path = "uploads/Image/"; string serverPath = Server.MapPath(path ...
- js 计算某年某周日期范围
<HTML><HEAD><script type="text/javascript"> //目前只判断了4位有效输入的年份,//日期输出格式已处 ...
- rman全库恢复到不同主机,不同实例名,不同目录下
一.配置目标主机的ip.hostname及与源端主机的连通性 1.配置目标主机IP 使用图形界面配置IP: administration----network---修改IP(指定静态IP) deact ...
- web后台获取不到session中的值(loading sessions from persistent storage),后改用JS传值
线上的程序似乎从session中取不到domain数据,重启了一下tomcat查看log日志发现,居然有报错.错误信息如下 22-Sep-2016 00:52:16.562 SEVERE [local ...
- hdu 1599 find the mincost route
http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...
- 【转】如何测试CTS4.0 -- 不错
原文网址:http://blog.csdn.net/subsist/article/details/7209341/ CTS4.0测试步骤 V1.2 (更新到CTS4.0 R3) 第一:平台准 ...
- CDT+Eclipse代码自动提示
1.查看GCC的版本:$gcc -v————————————————————————gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)———————— ...
- margin:0 auto 与 text-align:center 的区别(转载)
摘自:http://www.cnblogs.com/zhwl/p/3529473.html 基本概念: 1.text-align: 属性规定元素中的文本的水平对齐方式; 该属性通过指定行框与哪个点 ...
- 快速批量导入庞大数据到SQL SERVER数据库(ADO.NET)
原文地址:http://www.cnblogs.com/chenxizhang/archive/2008/11/11/1331060.html 如果你需要在程序中批量插入成千上万行的数据,你会怎么编写 ...