//直接上代码:
public static void main(String[] args) { List<Integer> list = new Vector<Integer>(20);
for (int i = 0; i < 10; i++) {
list.add(i % 3);
list.add(i % 6);
list.add(i % 2);
list.add(i % 4);
} System.out.println("原始元素:" + list);
List<Integer> reList = new Vector<Integer>(20);
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i) == list.get(j)) {
reList.add(list.remove(i));
i--;
break;
}
}
} // System.out.println("去重后:" + list);
// System.out.println("重复元素:" + reList);
// 输出:
// 原始元素:原始元素:[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 4, 0, 0, 2, 5, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2, 2, 0, 0, 0, 3, 1, 1]
// 去重后:[4, 5, 2, 0, 3, 1]
// 重复元素:[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 0, 0, 2, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2, 0, 0, 1] //去重方法2
Set<Integer> intset = new HashSet<Integer>(list);
System.out.println("set去重后" + intset);
// 输出:
// 原始元素:[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 0, 2, 0, 3, 1, 3, 1, 4, 0, 0, 2, 5, 1, 1, 0, 0, 0, 2, 1, 1, 1, 3, 2, 2, 0, 0, 0, 3, 1, 1]
// set去重后[0, 1, 2, 3, 4, 5] //数组
List<Integer> notReList = new Vector<Integer>(20);
int[] intarr = new int[]{1, 2, 4, 5, 6, 3, 4, 2, 3, 4, 34, 5, 23, 5, 2, 3, 4, 3, 3,99};
for (int i = 0; i < intarr.length ; i++) {//如果不计算notReList用i < intarr.length改成i < intarr.length-1
if (!notReList.contains(intarr[i])) {
notReList.add(intarr[i]);
}
for (int j = i + 1; j < intarr.length; j++) {
if (intarr[i] == intarr[j]) {
reList.add(intarr[i]);
break;
}
}
}
System.out.println("去重后:" + notReList);
System.out.println("重复元素:" + reList);
// 输出:
// 去重后:[1, 2, 4, 5, 6, 3, 34, 23, 99]
// 重复元素:[2, 4, 5, 3, 4, 2, 3, 4, 5, 3, 3]
// list和数组也可以互转:
// List intList=Arrays.asList(intarr);或
// Integer[] intarr2=new Integer[list.size()];
// list.toArray(intarr2);
    }

版权所有,转载请注明出处:http://www.cnblogs.com/langtianya/p/4676816.html

找出现有Vector或ArrayList或数组中重复的元素&给现有Vector或ArrayList或数组去重的更多相关文章

  1. php如何去掉二维数组中重复的元素?

    $arr=array("=>array("a","b")); 我想得到的结果是:只输出第一项(第一项和第三项相同,去第一项)和第二项这个怎么解决 ...

  2. java去除数组中重复的元素方法总结

    /* * ArrayUnique.java * Version 1.0.0 * Created on 2017年12月16日 * Copyright ReYo.Cn */ package reyo.s ...

  3. js删除数组中重复的元素

    1.方法一 将数组逐个搬到另一个数组中,当遇到重复元素时,不移动,若元素不重复则移动到新数组中 function unique(arr){ var len = arr.length; var resu ...

  4. array_unique() - 去除数组中重复的元素值

      array_unique() 定义和用法 array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名 ...

  5. JavaScript 找出数组中重复的元素

    实现检测数组重复元素的功能,需要注意一点的是,多个(2个或2个以上)重复元素,我们只需要挑出一个来就可以了. <!DOCTYPE html> <html> <head&g ...

  6. 删除数组中重复的元素(JSON)

    先上一个基础的: var a = [1,2,3,3,4]; var b = []; for (var i = 0; i < a.length; ++i) { if (b.indexOf(a[i] ...

  7. php 统计一维数组中重复的元素个数

    <?php echo "<pre>"; $array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12, ...

  8. php如何去掉二维数组中重复的元素

    $arr=array( "1"=>array("a","b "), "2"=>array("a&q ...

  9. PHP删除数组中重复的元素

    array_unique($arr): //删除重复元素 $arr = [1,2,3,0,1]; echo '<pre>'; var_dump($arr); $arr = array_un ...

随机推荐

  1. puer工具的使用

    在项目开发的过程当中,总会有前端开发快完成,后端接口却迟迟提供不了的情况.此时为了不影响前端开发的进度,我们可以借助puer来模拟后端接口测试.简单的说,puer就是一个可以实时刷新的前端服务器.具体 ...

  2. Stack around the variable 'szStr' was corrupted.

    错误:stack around the variable “XX” was corrupted.,中文翻译就是“在变量XX周围的堆栈已损坏”. 把 project->配置属性->c/c++ ...

  3. 学习笔记——Maven实战(一)坐标规划

    坐标是什么?为什么要规划? 坐标是Maven最基本的概念,它就像每个构件的身份证号码,有了它我们就可以在数以千万计的构件中定位任何一个我们感兴趣的构件.举个最简单的例子,如果没有坐标,使用JUnit的 ...

  4. HoloLens开发手记 - Unity之Gestures手势识别

    手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...

  5. 服务发现:Zookeeper vs etcd vs Consul

    [编者的话]本文对比了Zookeeper.etcd和Consul三种服务发现工具,探讨了最佳的服务发现解决方案,仅供参考. 如果使用预定义的端口,服务越多,发生冲突的可能性越大,毕竟,不可能有两个服务 ...

  6. $self $index $first $last parent() outerParent()

    index5.html <html><head> <title>$self $index $first $last parent() outerParent()&l ...

  7. jquery扩展函数详解(我的人生颠覆)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上次分析了jquery的源码 ...

  8. [渣翻译] 在ASP.NET MVC WebAPI项目中使用 AngularJS

    原文地址http://blog.technovert.com/2013/12/setting-up-angularjs-for-asp-net-mvc-n-webapi-project/ 我们最近发布 ...

  9. Bootstrap系列 -- 24. 下拉菜单基本用法

    在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“dropdown.js”的文 ...

  10. 由DataGridTextColumn不能获取到父级DataContext引发的思考

    在项目中使用DataGrid需要根据业务动态隐藏某些列,思路都是给DataGrid中列的Visibility属性绑定值来实现(项目使用MVVM),如下 <DataGridTextColumn H ...