Java从数组中删除重复项?

时间:2021-10-13 12:14:48

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.

我能够让我的try / catch工作并打印出电子邮件地址。但是,我不知道如何删除重复项。我还没有了解hashcode或如何使用Set。任何援助将不胜感激。

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 个解决方案

#1


31  

The Simple solution is that use Set of java,

简单的解决方案是使用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));

#2


4  

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];

String [] address = new String [100];

with this:

有了这个:

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

设置 addresses = new HashSet ();

And this:

和这个:

address[i] = email;

地址[i] =电子邮件;

with this:

有了这个:

addresses.add(email);

addresses.add(电子邮件);

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.

这几乎涵盖了它。想要一切都自动排序?用TreeSet替换上面的HashSet。现在去阅读这个优秀的教程,以便下次你可以更快地完成所有这些工作。

#3


3  

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

请将它们读入HashSet。这将为您处理重复项。

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.

将打印1。

#4


2  

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).

您可以尝试遍历数组中的每个元素,将其添加到另一个元素,检查第二个数组是否包含下一个项目,如果它确实跳过它。然后用第二个替换第一个数组。 (但在这种情况下,ArrayList更好)。

so something like this:

所以这样的事情:

List<String> FinalList = new ArrayList<String>();
for(string temp : adress)
{
if(!FinalList.contains(temp))
  FinalList.add(temp);
}

#5


1  

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.

根据需要使用ArrayUtil类。除了删除重复项之外,我还编写了一些方法。在不使用任何Collection框架类的情况下实现此类。

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

#6


0  

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)*/

#7


0  

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

#8


-1  

the first thing that comes into my head is to sort the array and then to check if the next element equals the current element. if so, delete the current element.

我想到的第一件事是对数组进行排序,然后检查下一个元素是否等于当前元素。如果是这样,删除当前元素。

oh and when you don´t know how many emails are stored in the file, an array is probably not the best way. I´d take some sort of list, so that i don´t have to care how many e-mail addresses are in the file.

哦,当你不知道文件中存储了多少封电子邮件时,数组可能不是最佳方式。我会采取某种列表,所以我不必关心文件中有多少个电子邮件地址。

#9


-1  

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

你可以编写一个在数组上运行的函数,一次只收一封电子邮件,当它找到相同的地址时,只需将其设置为null。当您在阵列上运行以进行打印时,请创建条件以仅在非空时打印电子邮件

#1


31  

The Simple solution is that use Set of java,

简单的解决方案是使用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));

#2


4  

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];

String [] address = new String [100];

with this:

有了这个:

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

设置 addresses = new HashSet ();

And this:

和这个:

address[i] = email;

地址[i] =电子邮件;

with this:

有了这个:

addresses.add(email);

addresses.add(电子邮件);

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.

这几乎涵盖了它。想要一切都自动排序?用TreeSet替换上面的HashSet。现在去阅读这个优秀的教程,以便下次你可以更快地完成所有这些工作。

#3


3  

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

请将它们读入HashSet。这将为您处理重复项。

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.

将打印1。

#4


2  

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).

您可以尝试遍历数组中的每个元素,将其添加到另一个元素,检查第二个数组是否包含下一个项目,如果它确实跳过它。然后用第二个替换第一个数组。 (但在这种情况下,ArrayList更好)。

so something like this:

所以这样的事情:

List<String> FinalList = new ArrayList<String>();
for(string temp : adress)
{
if(!FinalList.contains(temp))
  FinalList.add(temp);
}

#5


1  

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.

根据需要使用ArrayUtil类。除了删除重复项之外,我还编写了一些方法。在不使用任何Collection框架类的情况下实现此类。

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

#6


0  

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)*/

#7


0  

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

#8


-1  

the first thing that comes into my head is to sort the array and then to check if the next element equals the current element. if so, delete the current element.

我想到的第一件事是对数组进行排序,然后检查下一个元素是否等于当前元素。如果是这样,删除当前元素。

oh and when you don´t know how many emails are stored in the file, an array is probably not the best way. I´d take some sort of list, so that i don´t have to care how many e-mail addresses are in the file.

哦,当你不知道文件中存储了多少封电子邮件时,数组可能不是最佳方式。我会采取某种列表,所以我不必关心文件中有多少个电子邮件地址。

#9


-1  

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

你可以编写一个在数组上运行的函数,一次只收一封电子邮件,当它找到相同的地址时,只需将其设置为null。当您在阵列上运行以进行打印时,请创建条件以仅在非空时打印电子邮件