java删除数组中重复元素

本文介绍几种在Java中有效移除数组中重复元素的方法,包括使用Set、List及迭代检查等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

id="BAIDU_DUP_fp_iframe" src="https://pos.baidu.com/wh/o.htm?ltr=">
>
src="http://v3.jiathis.com/code/jiathis_utility.html">

Java SE

GXW33 GXW33 10-08 14:30
等级 T1 28次回复

java删除数组中重复元素

java删除数组中重复元素

id="iframeu2634446_0" src="http://pos.baidu.com/hcrm?sz=1366x60&rdid=2634446&dc=3&di=u2634446&dri=0&dis=0&dai=1&ps=338x0&coa=at%3D3%26pat%3D15%26tn%3Dtemplate_inlay_all_mobile_lu_native%26rss1%3D%2523FFFFFF%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26ptFS%3D16%26ptFC%3D%2523000000%26ptFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26ptFW%3D1%26conpl%3D0%26conpr%3D1%26conpt%3D0%26conpb%3D0%26rsi1%3D60%26ptn%3D1%26ptp%3D0%26itecpl%3D10%26piw%3D0%26pih%3D0%26ptDesc%3D0%26ptLogo%3D0&dcb=___adblockplus&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1492612855723&ti=CSDN%20%E8%AE%BA%E5%9D%9B&ari=2&dbv=2&drs=3&pcs=1349x611&pss=1349x13497&cfv=0&cpl=27&chi=1&cce=true&cec=UTF-8&tlm=1492612855&rw=611&ltu=https%3A%2F%2F2.zoppoz.workers.dev%3A443%2Fhttp%2Fbbs.csdn.net%2Fwap%2Ftopics%2F370233547&ecd=1&uc=1366x728&pis=-1x-1&sr=1366x768&tcn=1492612856&qn=f762961fcc8e15c6&tt=1492612855696.33.136.140" width="1366" height="60" align="center,center" vspace="0" hspace="0" scrolling="no">
soli11722984 soli11722984 10-08 14:30
等级 T1 1楼

倒到SET裏面去

woaini314 woaini314 10-08 14:35
等级 T1 2楼

element as map key

chruan chruan 10-08 14:37
等级 T1 3楼

可以排序,再删除

ForeverLonely00 ForeverLonely00 10-08 15:34
等级 T1 4楼

int[] arr = new int[]{1,2,3,4,23,3,5,1};
ArrayList<Integer> arrList = new ArrayList<Integer>();
for(int i=0; i<arr.length; i++)
{
if(!arrList.contains(arr[i]))
arrList.add(arr[i]);
}
Iterator<Integer> inter = arrList.iterator();
while(inter.hasNext())
System.out.println(inter.next());

jiangxiayang jiangxiayang 10-08 15:40
等级 T1 5楼

package demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Shanchu {
public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
List<Integer> numList = new ArrayList<Integer>();
for (int i : nums)
numList.add(i);
System.out.println(numList);

//做删除
Iterator<Integer> it = numList.iterator();
int temp = -1;
if (it.hasNext())
temp = it.next();
while (it.hasNext()) {
int i = it.next();
if (i == temp) {
it.remove();
} else {
temp = i;
}
}
System.out.println(numList);
}

}

soli11722984 soli11722984 10-08 15:52
等级 T1 6楼

都這麽複雜啊


public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
List<Integer> numList = new ArrayList<Integer>();
for (int i : nums)
numList.add(i);
Set<Integer> numSet = new HashSet<Integer>();
numSet.addAll(numList);
System.out.println(numSet);
}

feifeikub feifeikub 10-08 15:53
等级 T1 7楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

顶一个

changtianshuiyue changtianshuiyue 10-08 16:29
等级 T1 8楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

牛人,膜拜下,怎么想到的

lliiqiang lliiqiang 10-08 17:22
等级 T1 9楼

从第一个遍历,查看前面是否有重复的,有就删除

dy110936 dy110936 10-08 17:30
等级 T1 10楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊

Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nums)
 ……

先顶一下,但是这样顺序不就乱了么。

jiunizhuai jiunizhuai 10-08 17:46
等级 T1 11楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊

Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nums)
 ……



这个不错,通过集合转换,用treeset 还可以排序,呵呵

TKD03072010 TKD03072010 10-08 17:47
等级 T1 12楼

用set集合就行了

taodengwen taodengwen 10-08 18:04
等级 T1 13楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nu……

zzz~~

walkman_22 walkman_22 10-09 09:22
等级 T1 14楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nu……


我也是这个思路。

cbxjj cbxjj 10-09 09:29
等级 T1 15楼

不用set也很容易的

public static void main(String[] args) {
String[] s = {"1","10","15","14","111","133","12","13","1","13"};
List<String> l = new ArrayList<String>();
for(String a:s){
if(!l.contains(a)){
l.add(a);
}
}
System.out.println(l);
}

liyangyun1986 liyangyun1986 10-09 09:32
等级 T1 16楼

最简单的方法就是丢到set集合中去~

gl74gs48 gl74gs48 10-09 10:17
等级 T1 17楼


import java.util.*;
class TestSet 
{
public static void main(String[] args) 
{
Integer[] nums = { 5, 5, 6, 6, 6, 8, 8, 7, 11, 12, 12 };
HashSet hset = new HashSet(Arrays.asList(nums));
Iterator i = hset.iterator();
while(i.hasNext()){
System.out.println(i.next());
}            

}
}

ksqqxq ksqqxq 10-09 10:33
等级 T1 18楼

牛人太多了,膜拜中。。。。。。

chcchb chcchb 10-09 11:15
等级 T1 19楼

set方法不错

snowday88 snowday88 10-09 12:25
等级 T1 20楼

Set有一个构造方法 参数就是list

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

dfox_java dfox_java 10-09 17:18
等级 T1 21楼


    
//简单的for循环
for(int i=1;i<arr.length;i++){
  for(j=0;j<i;j++){
     if(arr[i]==arr[j]){
       随便干嘛。。
        

}
}
}

wingson_shen wingson_shen 10-09 17:39
等级 T1 22楼

我们项目中的工具类,用来去掉List中空值和相同项的。


public <T> List<T> removeSameItem(List<T> list) {
List<T> difList = new ArrayList<T>();
for(T t : list){
if(t != null && !difList.contains(t)){
difList.add(t);
}
}
return difList;
}


leisore leisore 10-09 18:45
等级 T1 23楼

引用 5 楼 jiangxiayang 的回复:
package demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Shanchu {
public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, ……

再稍微简单点的(不考虑性能的话):

    Integer[] nums = { 5, 6, 6, 6, 8, 8, 7 };
    Set<Integer> numSet = new LinkedHashSet<Integer>(Arrays.asList(nums));
    System.out.println(numSet);

afgasdg afgasdg 10-09 22:47
等级 T1 24楼

一句话搞定:
System.out.println(new LinkedHashSet<Integer>(Arrays.asList(5, 6, 6, 6, 8, 8, 7)));

WWdsaf5977129 WWdsaf5977129 10-09 22:59
等级 T1 25楼

牛人多,继续拜读。。。。。。。。。

fuwenhai fuwenhai 10-10 09:33
等级 T1 26楼

先把数组中元素放入set集合中啊,然后从set中取出来,得到的就是去除重复数据之后的

jesus7_wei jesus7_wei 10-10 13:44
等级 T1 27楼

int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i : nums) {
            set.add(i);
        }

zxabc332335 zxabc332335 10-10 15:31
等级 T1 28楼

public static void main(String[] args) {
        String[] s = {“1”,”10”,”15”,”14”,”111”,”133”,”12”,”13”,”1”,”13”};
        List<String> l = new ArrayList<String>();
        for(String a:s){
            if(!l.contains(a)){
                l.add(a);
            }
        }
        System.out.println(l);
    }


0 0 回复 分享
img
(function(){
        $('#share_btn').click(function(){
            $('.popup_cover').stop().show();
            $('.sharePopup_box').stop().slideDown();
        });
        $('.sharePopup_cancel').click(function(){
            $('.popup_cover').stop().hide();
            $('.sharePopup_box').stop().slideUp();
        });
    });
('.topic-expired').click(function(){ alert("帖子太久远,不提供回复功能"); });

      <li><a href="/" class="J_nav"><i class="left_dot">•</i><span>首页</span></a></li>

      <li><a href="javascript:;" class="J_nav"><i class="left_dot">•</i><span>我感兴趣的论坛</span><i class="iconfont icon_down"></i><i class="iconfont icon_up"></i></a>

                <ul class="sub_nav">
                 <li>

·基础类


#popup_mask { position: absolute; width: 100%; height: 100%; background: #000; z-index: 9999; left: 0px; top: 0px; opacity: 0.3; filter: alpha(opacity=30); display: none; }
img 即使是一小步
也想与你分享
打开
img
(function(){('#search_wap_topic').click(function(e) { console.log(12123123); e.preventDefault(); $('#search_wap_topics').submit(); }); });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值