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 ...
随机推荐
- 修改EF的默认约定模型的方式
EF默认使用约定的模型,但是有时类不遵从约定,我们需要能够执行进一步的配置.对此有两种方法:数据注释和Fluent API 注释 class User { [Key] public int Key{ ...
- 通过maven创建自己的archetype
最近项目组做好一套框架,为了推广需要创建一些空白项目给项目组使用,因为所有的空白项目里面的配置基本上都是一样的,为了减少重复工作,想通过maven创建一个自己的archetype,于是在网上大致搜了一 ...
- iOS~~MD5加密
// 一般加密 +(NSString *)md5String:(NSString *)str { const char *password=[str UTF8String]; unsigned cha ...
- Fedora下YouCompleteMe配置
需要在默认的.ycm_extra_conf.py 中添加(C++的路径可能需要根据版本号修改) '-isystem', '/usr/include', '-isystem', '/usr/includ ...
- libcurl下载文件简易demo
size_t test_save(void *ptr, size_t size, size_t nmemb, void *stream) { size_t sizes = size * nmemb; ...
- ifstream中文路径问题分析
最近维护一个项目,遇到了ifstream在中文路径下打开文件失败的bug,我搜索了一下,最后整理成下文以后日后查阅. 一.问题重现 vs2008下创建一个简单win32工程. 使用ANSI编码方式:项 ...
- Hdu1093
#include <stdio.h> int main() { int T,n; ; while(scanf("%d",&T)!=EOF){ while(sca ...
- Unity3D TouchScript 插件教程一
只是个人学习小记,谈不上教程,但是为了命中搜索引擎关键词,只好装逼了:),可能对于大家来说太简单了吧,网上中文教程没搜到 ,只好自己摸索了. 插件资源下载地址:https://www.assetsto ...
- Is there a complete List of JVM exit codes
Argument passed to System.exit(x) -> becomes the JVM exit code. Exit code 0 is used to indicate n ...
- keil C 应注意的几个问题
我们使用Keil C调试某系统时积累的一些经验 1.在Windows2000下面,我们可以把字体设置为Courier,这样就可以显示正常.2.当使用有片外内存的MCU(如W77E58,它有1K片外内存 ...