Remove duplicates from integer array

前端 未结 23 2365
执念已碎
执念已碎 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:18
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    // Remove duplicates from a list of integers
    public class IntegerUtils {
        public static void main(String[] args) {
            int intArray[] = {1, 2, 4, 2, 67, 4, 9};
            List<Integer> uniqueList = removeDuplicates(intArray);
            uniqueList.stream().forEach(p -> System.out.println(p));
        }
    
        public static List<Integer> removeDuplicates(int[] intArray) {
            return Arrays.stream(intArray).boxed().distinct().collect(Collectors.toList());
        }
    }
    
    0 讨论(0)
  • 2020-12-01 19:19
    public int[] removeRepetativeInteger(int[] list){
            if(list.length == 0){
                return null;
            }
            if(list.length == 1){
                return list;
            }
    
        ArrayList<Integer> numbers = new ArrayList<>();
        for(int i = 0; i< list.length; i++){
            if (!numbers.contains(list[i])){
                numbers.add(list[i]);
            }
        }
        Iterator<Integer> valueIterator = numbers.iterator();
        int[] resultArray = new int[numbers.size()]; 
        int i = 0;
        while (valueIterator.hasNext()) {
            resultArray[i] = valueIterator.next();
            i++;
        }
        return resultArray;     
    
    }
    
    0 讨论(0)
  • 2020-12-01 19:22

    Iterate over the array and populate a set because sets cannot contain duplicates. Then copy the elements from the set into a new array and return it. This is shown below:

    public static int[] removeDuplicates(int[] array) {
        // add the ints into a set
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
    
        // copy the elements from the set into an array
        int[] result = new int[set.size()];
        int i = 0;
        for (Integer u : set) {
            result[i++] = u;
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-01 19:22

    Try this

    public static int[] removeDuplicates(int[] s) {     
        Integer[] array = new HashSet<Integer>(Arrays.asList(ArrayUtils.toObject(s))).toArray(new Integer[0]);      
        return ArrayUtils.toPrimitive(array);
    }
    

    Edit: Updated with Apache Lang to convert to primitives.

    0 讨论(0)
  • 2020-12-01 19:25

    You can do naively though. First you need to sort the array. You can do it using any of sorting algorithms. I did use quick sort. And then check a position with its next position. If they are not same, add value in a new array, otherwise skip this iteration.

    Sample Code (Quick Sort):

     public static void quickSort(int[] array, int low, int high) {
        int i = low;
        int j = high;
    
        int pivot = array[low + (high - low) / 2];
    
        while (i <= j) {
            while (array[i] < pivot) i++;
            while (array[j] > pivot) j--;
            if (i <= j) {
                exchange(array, i, j);
                i++;
                j--;
            }
        }
        if (0 < j) quickSort(array, 0, j);
        if (i < high) quickSort(array, i, high);
    }
    
    public static void exchange(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    

    Remove duplicates:

     public static int[] removeDuplicate(int[] arrays) {
        quickSort(arrays, 0, arrays.length - 1);
    
        int[] newArrays = new int[arrays.length];
        int count = 0;
        for (int i = 0; i < arrays.length - 1; i++) {
            if (arrays[i] != arrays[i + 1]) {
                newArrays[count] = arrays[i];
                count++;
            }
        }
        return newArrays;
    }
    
    0 讨论(0)
  • 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<Integer> 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.

    0 讨论(0)
提交回复
热议问题