Java Remove Duplicates from an Array?

2019-01-01 02:26发布

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++;
         }
      }
   }
}

9条回答
像晚风撩人
2楼-- · 2019-01-01 02:52

You can try going through each element in the array, adding it to another one, checking if the 2nd array contains the next item, if it does skip it. Then just replace the 1st array with the 2nd. (ArrayList is better in this case though).

so something like this:

List<String> FinalList = new ArrayList<String>();
for(string temp : adress)
{
if(!FinalList.contains(temp))
  FinalList.add(temp);
}
查看更多
十年一品温如言
3楼-- · 2019-01-01 02:52

you can write a function that run on the array and take one email at a time and when ever it find the same address just set it to null. when you're running on the array to print it, make a condition to print the email only if its not null

查看更多
查无此人
4楼-- · 2019-01-01 02:53

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.

查看更多
一个人的天荒地老
5楼-- · 2019-01-01 02:58

If you want to remove duplicates you can try something like this:

String[] address = new String[100]; // the array that contains all addresses
ArrayList<String> uniqueAddresses = new ArrayList<String>(); // create arraylist to contain all non-repeated addresses
for(String addr : address){ // cycle through the entire array
   if(!uniqueAddresses.contain(addr)){ // check if the address already there
      uniqueAddresses.add(addr); // add it
   }
}
查看更多
长期被迫恋爱
6楼-- · 2019-01-01 03:06

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

Set<String> addresses = new HashSet<String>();
addresses.add("a@a.com");
addresses.add("a@a.com");
addresses.add("a@a.com");
System.out.println(addresses.size());

Will print 1.

查看更多
残风、尘缘若梦
7楼-- · 2019-01-01 03:06

Please use below code for remove duplicates in an integer array.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test123;

import java.util.ArrayList;
import java.util.HashSet;

/**
 *
 * @author krawler
 */
public class Test123 {

    /**
     * @param args the command line arguments
     */
     public static ArrayList<Integer> removeDuplicates(ArrayList<Integer> list) {

	// Store unique items in result.
	ArrayList<Integer> result = new ArrayList<>();

	HashSet<Integer> set = new HashSet<>();

	
	for (Integer item : list) {

	   
	    if (!set.contains(item)) {
		result.add(item);
		set.add(item);
	    }
	}
	return result;
    }

    public static void main(String[] args) {

	ArrayList<Integer> list = new ArrayList<>();
	list.add(12);
	list.add(12);
	list.add(8);
	list.add(6);
	list.add(4);
	list.add(4);
        list.add(2);
        list.add(1); 
           //int a[]={12,12,8,6,4,4,2,1}
	
	ArrayList<Integer> unique = removeDuplicates(list);
	for (int element : unique) {
	    System.out.println(element);
	}
    }
}

/*run:
12
8
6
4
2
1
BUILD SUCCESSFUL (total time: 0 seconds)*/

查看更多
登录 后发表回答