Remove duplicates from integer array

Solution 1:

To Preserve the ordering and to remove duplicates in the integer array, you can try this:

public void removeDupInIntArray(int[] ints){
    Set<Integer> setString = new LinkedHashSet<Integer>();
    for(int i=0;i<ints.length;i++){
        setString.add(ints[i]);
    }
    System.out.println(setString);
}

Hope this helps.

Solution 2:

try this -

public static int[] removeDuplicates(int []s){
    int result[] = new int[s.length], j=0;
    for (int i : s) {
        if(!isExists(result, i))
            result[j++] = i;
    }
    return result;
}

private static boolean isExists(int[] array, int value){
    for (int i : array) {
        if(i==value)
            return true;
    }
    return false;
}

Solution 3:

First of all, you should know length without duplicates(dups): initial length minus number of dups. Then create new array with right length. Then check each element of list[] for dups, if dup founded - check next element, if dup not founded - copy element to new array.

public static int[] eliminateDuplicates(int[] list) {
    int newLength = list.length;
    // find length w/o duplicates:
    for (int i = 1; i < list.length; i++) {
        for (int j = 0; j < i; j++) {
            if (list[i] == list[j]) {   // if duplicate founded then decrease length by 1
                newLength--;
                break;
            }
        }
    }

    int[] newArray = new int[newLength]; // create new array with new length
    newArray[0] = list[0];  // 1st element goes to new array
    int inx = 1;            // index for 2nd element of new array
    boolean isDuplicate;

    for (int i = 1; i < list.length; i++) {
        isDuplicate = false;
        for (int j = 0; j < i; j++) {
            if (list[i] == list[j]) {  // if duplicate founded then change boolean variable and break
                isDuplicate = true;
                break;
            }
        }
        if (!isDuplicate) {     // if it's not duplicate then put it to new array
            newArray[inx] = list[i];
            inx++;
        }
    }
    return newArray;
}

Solution 4:

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.