Remove duplicates from integer array

前端 未结 23 2380
执念已碎
执念已碎 2020-12-01 18:52

I having a problem with coding this:

Write a static method named removeDuplicates that takes as input an array of integers and returns as a result a new

23条回答
  •  爱一瞬间的悲伤
    2020-12-01 19:26

    Maybe you can use lambdaj (download here,website), this library is very powerfull for managing collections (..list,arrays), the following code is very simple and works perfectly:

    import static ch.lambdaj.Lambda.selectDistinct;
    import java.util.Arrays;
    import java.util.List;
    
    public class DistinctList {
         public static void main(String[] args) {
             List numbers =  Arrays.asList(1,3,4,2,1,5,6,8,8,3,4,5,13);
             System.out.println("List with duplicates: " + numbers);
             System.out.println("List without duplicates: " + selectDistinct(numbers));
         }
    }
    

    This code shows:

    List with duplicates: [1, 3, 4, 2, 1, 5, 6, 8, 8, 3, 4, 5, 13]
    List without duplicates: [1, 2, 3, 4, 5, 6, 8, 13]
    

    In one line you can get a distinct list, this is a simple example but with this library you can resolve more.

    selectDistinct(numbers)
    

    You must add lambdaj-2.4.jar to your project. I hope this will be useful.

    Note: This will help you assuming you can have alternatives to your code.

提交回复
热议问题