美文网首页
Java实现数组去重

Java实现数组去重

作者: 多听音乐多喝水 | 来源:发表于2018-10-15 19:36 被阅读0次

思路就是新建一个数组,把原数组的元素赋进去,再去除因此产生的0。

import java.util.Arrays;

public class Solution {

public static void main(String[] args) {
    int[] array1 = {1,2,3,4,4};
    int[] array2 = {3,1,4,1,5,9,2,6,5,3,5,8,9,3,9};
    int[] array3 = {1,1,1,1};
    showNonDuplicateArray(array1);
    showNonDuplicateArray(array2);
    showNonDuplicateArray(array3);
}


public static void showNonDuplicateArray(int[] a) {
    int[] newArr = new int[a.length];

    int index = 0; // 新数组存储元素索引(或者说无重复的个数)

    outer: for (int i = 0; i < a.length; i++) {
        for (int j = i + 1; j < a.length; j++) {
            //当数据重复时,跳出外圈循环
            if (a[i] == a[j]) {
                continue outer;
            }
        }
        // 后面没有与当前元素重复的值,保存这个数
        newArr[index] = a[i];
        index++;
    }
    // 新数组中存储着无重复的值和后面一些0
    int[] result = new int[index];
    for (int i = 0; i < index; i++) { // 遍历有效值个数
        result[i] = newArr[i];
    }
    System.out.println(Arrays.toString(result));


}

}

相关文章

  • Java实现数组去重

    思路就是新建一个数组,把原数组的元素赋进去,再去除因此产生的0。

  • Array集结号

    实现数组去重的几种方法 数组去重一 数组去重二 利用数组indexof+push实现数组去重 数组去重三 利用对象...

  • JS实现数组去重常用的六种方法

    双重for循环去重 includes实现数组去重 indexOf实现数组去重 利用set方法去重 ES6 Arra...

  • js reduce去重用法

    reduce不仅仅可以数据累加,还可以实现去重效果。 重复次数计算 数组去重 数组对象去重,转为数组 对象去重

  • Java 数组去重

    JAVA中List对象去除重复值,大致分为两种情况,一种是List 、List 这类,直接根据List中的值进行去...

  • 实现数组去重

  • 2018-06-13(数组 去重,class类名兼容)

    数组 去重 (使用josn的特点来实现数组的去重) class 类名 的 兼容 function $(){ ...

  • 常见数组操作方法(容易混乱)

    filter方法巧妙实现数组去重

  • Java数组去重问题

    方法一: 使用两个标志位进行标定去重。此方法无需使用任何容器,也不需要另外开辟数组空间,推荐使用,但丢失了数组元素...

  • java 对象数组去重

    java对象数组去重; 将原数组插入到新数组的时候,将插入的对象和新数组中的已插入对象进行比较,若不相同,则插入到...

网友评论

      本文标题:Java实现数组去重

      本文链接:https://www.haomeiwen.com/subject/olrqzftx.html