美文网首页
数组去重

数组去重

作者: 霍运浩 | 来源:发表于2019-04-24 22:17 被阅读0次

题目描述

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A =[1,1,2],

Your function should return length =2, and A is now[1,2].

解题思路

构造一个List集合,每次add元素先看list集合存在该数字嘛,存在则不add,否则则add。

代码实现

import java.util.*;

public class Main {
    public int removeDuplicates(int[] A) {
       if(A==null || A.length<=1){
           return 0;
       }
       List<Integer> list=new ArrayList<Integer>();
       for(int i=0;i<A.length;i++){
            if(list.contains(A[i])){
                continue;
            }
            list.add(A[i]);
       }
        return list.size();
    }
}

相关文章

  • Array集结号

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

  • 实现数组去重有哪些方式

    简单的数组去重 数组对象去重

  • 数组去重的四种方法

    利用双for循环去重 利用对象数组去重 利用对象数组去重并且记录重复次数 通过创建一个新数组进行数组去重

  • js数组去重、对象数组去重

    普通数组去重 一、普通数组去重 方法一:遍历数组法 方法二:排序法 方法三:对象法 对象数组去重 方法一:将对象数...

  • javascript数组去重,数组对象去重

    利用Reduce去重 function unique(arr) {var obj = {};arr = arr.r...

  • js:数组去重

    数组去重的常见写法: 数组去重封装成方法: es6的数组去重(Array.from):

  • ES6数组去重

    普通数组去重 方法1 方法2 对象数组去重

  • js reduce去重用法

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

  • 数组去重

    传统方法 ES6 扩展 传统方法 最后再写到 Array.prototype 原型中

  • 数组去重

    老题了。。虽然网上一搜一大堆,还是自己想了想,自己动笔写了几种。

网友评论

      本文标题:数组去重

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