Java Remove Duplicates from an Array?

46,411

Solution 1

The Simple solution is that use Set of java,

so set remove duplicate value automatically

and in your code you have array than convert array to set directly using code

Set<T> mySet = new HashSet<T>(Arrays.asList(someArray));

Solution 2

Learn Set. The time it will take you to learn it is less than the time it will take you to code something that doesn't use it.

I'll get you started. Replace this:

String[] address = new String[100];

with this:

Set<String> addresses = new HashSet<String>();

And this:

address[i] = email;

with this:

addresses.add(email);

You don't need the i anymore.

You're done. If you'd like to print everything out:

for (String address : addresses) {
     System.out.println (address);
}

That pretty much covers it. Want everything to be automatically sorted? Replace the HashSet above with TreeSet. Now go read this excellent tutorial so that next time, you can get it all done faster and on your own.

Solution 3

Read them into a HashSet instead. This will handle duplicates for you.

Set<String> addresses = new HashSet<String>();
addresses.add("[email protected]");
addresses.add("[email protected]");
addresses.add("[email protected]");
System.out.println(addresses.size());

Will print 1.

Solution 4

Use the ArrayUtil class as you need. I have written some methods other than removing duplicates. This class is implemented without using any Collection framework classes.

public class ArrayUtils {
/**
 * Removes all duplicate elements from an array. 
 * @param arr Array from which duplicate elements are to be removed.
 * @param removeAllDuplicates true if remove all duplicate values, false otherwise 
 * @return Array of unique elements.
 */
public static int[] removeDuplicate(int[] arr, boolean removeAllDuplicates)         {
    int size = arr.length;

    for (int i = 0; i < size;) {
        boolean flag = false;

        for (int j = i + 1; j < size;) {
            if (arr[i] == arr[j]) {
                flag = true;
                shrinkArray(arr, j, size);
                size--;
            } else
                j++;
        }

        if (flag && removeAllDuplicates) {
            shrinkArray(arr, i, size);
            size--;
        } else
            i++;
    }

    int unique[] = new int[size];
    for (int i = 0; i < size; i++)
        unique[i] = arr[i];

    return unique;
}

/**
 * Removes duplicate elements from an array. 
 * @param arr Array from which duplicate elements are to be removed.
 * @return Array of unique elements.
 */
public static int[] removeDuplicate(int[] arr) {
    return removeDuplicate(arr, false);
}


private static void shrinkArray(int[] arr, int pos, int size) {
    for (int i = pos; i < size - 1; i++) {
        arr[i] = arr[i + 1];
    }
}

/**
 * Displays the array.
 * @param arr The array to be displayed.
 */
public static void displayArray(int arr[]) {
    System.out.println("\n\nThe Array Is:-\n");

    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + "\t");
    }
}

/**
 * Initializes the array with a given value.
 * @param arr The array to be initialized.
 * @param withValue The value with which the array is to be initialized.
 */
public static void initializeArray(int[] arr, int withValue) {
    for (int i = 0; i < arr.length; i++) {
        arr[i] = withValue;
    }
}

/**
 * Checks whether an element is there in the array. 
 * @param arr The array in which the element is to be found.
 * @param element The element that is to be found.
 * @return True if found false otherwise
 */
public static boolean contains(int arr[], int element) {
    for(int i=0; i< arr.length; i++) {
        if(arr[i] == element)
            return true;
    }

    return false;
}

/**
 * Removes a element from an array.
 * @param arr The array from which the element is to removed.
 * @param element The element to be removed
 * @return The size of the array after removing.
 */
public static int removeElement(int[] arr, int element) {
    int size = arr.length;
    for(int i=0; i< arr.length; i++){
        if(arr[i] == element){
            shrinkArray(arr, i, arr.length);
            size--;
        }
    }
    return size;
}

/**
 * Counts unique elements in an array.
 * @param arr The required array.
 * @return Unique element count.
 */
public static int uniqueElementCount(int arr[]) {
    int count = 0;
    int uniqueCount=0;
    int[] consideredElements = new int[arr.length];

    initializeArray(consideredElements, 0);

    for(int i=0;i<arr.length;i++) {
        int element = arr[i];
        for(int j=i+1;j<arr.length; j++){
            if(element != arr[j] && !contains(consideredElements, element)){
                consideredElements[count++] = element;
            }
        }
    }

    for(int i=0;i< consideredElements.length;i++)
        if(consideredElements[i]!=0)
            uniqueCount++;

    return uniqueCount;
}
}
Share:
46,411
Bean Winz
Author by

Bean Winz

Updated on July 23, 2022

Comments

  • Bean Winz
    Bean Winz almost 2 years

    I am supposed to read in a file containing many different email addresses and print them out using an array. The problem is I need to eliminate duplicate emails.

    I was able to get my try / catch working and print out the email addresses. However, I am not sure how to go about removing the duplicates. I do not have an understanding of hashcode's or how to use a Set yet. Any assistance would be appreciated.

    Here is what I have so far:

    import java.util.Scanner;
    import java.io.*;
    
    public class Duplicate {
       public static void main(String[] args) {
    
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Enter file name: ");
          String fileName = keyboard.nextLine();
          if (fileName.equals("")) {
             System.out.println("Error: User did not specify a file name.");
          } else {
             Scanner inputStream = null;
    
             try {
                inputStream = new Scanner(new File(fileName));
             } catch (FileNotFoundException e) {
                System.out.println("Error: " + fileName + " does not exist.");
                System.exit(0);
             }
    
             String[] address = new String[100];
    
             int i = 0;
             while (inputStream.hasNextLine()) {
                String email = inputStream.nextLine();
                // System.out.println(email);
    
                address[i] = email;
                System.out.println(address[i]);
                i++;
             }
          }
       }
    }
    
  • Bean Winz
    Bean Winz about 12 years
    thanks. I guess I can try it-i've just never even heard of sets before as I am just beginning programming. So what is the <T> and what does the .asList do?
  • Yogesh Prajapati
    Yogesh Prajapati about 12 years
    hi Bean, if you use Set mySet = new HashSet(Arrays.asList(someArray)); than its ok but if you want to create a set of specific datatype so you can give datatype instead of T ex Set<String> set = new Hashset<String>();.
  • sparc_spread
    sparc_spread about 12 years
    The T is a placeholder for whichever datatype you want to use (String in your case). This notation is a bit advanced for someone just starting out so don't worry about it for right now. Learn basics like this collections tutorial first and the T stuff will seem a bit more natural when you encounter it.
  • Bean Winz
    Bean Winz about 12 years
    thanks i just figured that part out. But I am confused on where to put this code. would I put it in my while loop?
  • Alex D
    Alex D about 12 years
    No. First build up the array in your while loop, then after the array is finished, convert it into a set. Then go over the set, and print all the addresses out. You can use something like for(String s : mySet) System.out.println(s);
  • Bean Winz
    Bean Winz about 12 years
    alright. Also how do I make it so it is case INsensitive. so for example: [email protected] would be a duplicate of [email protected]. I know i would do .equalsIgnoreCase but i'm not sure where
  • sparc_spread
    sparc_spread about 12 years
    Can you tell me a bit more about the constraints of the assignment? I'm surprised that they only let you use arrays, considering that the list of addresses appears to be of arbitrary length (vs. the fixed length of an array).