How to find duplicate string from string array

I have an array of strings that contains a list of strings. I want to find out if there are duplicate entries in this list. I basically have a list of users, and there should be no duplicate entries.

+6
source share
9 answers

you can add a String array to a HashSet

Set<String> h = new HashSet<String>(Arrays.asList(new String[] { "a", "b" })); 

this will give you unique String values. Convert the HashSet back to an array if necessary

 String[] uniqueValues = h.toArray(new String[0]); 
+5
source

If you need unique things, we have Set in java

  String[] users = "User1,User2,User1,User,User".split(","); Set<String> uniquUsers = new HashSet<String>(); for (int i = 0; i < users.length; i++) { if (!uniquUsers.add(users[i])) users[i] = "Duplicate"; // here I am assigning Duplicate instead if find duplicate // you can assign as null or whatever you want to do with duplicates. } System.out.println(Arrays.toString(users)); 
+6
source

Add them to the set and you will get unique users. Then convert it back to an array.

+2
source

Sort alphabetically. If any two adjacent entries are the same, you have found a duplicate.

0
source

If you want to test adding a new user, you just need to start the array and use username.equals(*) for each existing user.

If you have an array with duplicate entries, just run this algorithm for every user you have.

These are crude methods, there are many optimizations for this problem.

0
source

as you mentioned, there should be no duplicate entries, so it’s better to repeat the entire array before adding a new user, rather than adding and then checking for duplicates. the first solution will solve it in O (N).

0
source

The idea of ​​Patash seems the simplest. You can use Arrays.sort() to easily and efficiently sort the array.

If you really want a SEARCH, you are probably using one of the Arrays.binarysearch() methods. But they also require sorted arrays .... For each element of your array (say, by index n), find the 0 ... (n-1) section, and also search for the part (n + 1) ... (length - 1), but that would be extremely useless if you could just compare with one element adjacent to n. So, back to the previous sentence.

If you want to code a little bit, perhaps due to speed, you can use the contains() method of one of the implementing AbstractCollection classes - perhaps ArrayList (may contain duplicates), TreeSet (sorted, contains unique values) or HashSet (unsorted, contains unique values). You can call the constructor for these collections with the Arrays.asList(yourArray) parameter, so you do not need to fill one by one.

As ay89 rightly mentions, it's easier to have an array with unique values ​​(lots of other words), and then check to see if your value is set before trying to add it. Makes things a lot easier. But you may not always have that luxury with what is given to you.

0
source

create an array news_data and add lines to it.

 for (int i = 0; i < news_data.length; i++) { for (int j = i+1; j < news_data.length; j++) { if(news_data[i].equals(news_data[j])){ news_data = removeElement(news_data, j); } } } public static String[] removeElement(String[] original, int element){ String[] n = new String[original.length - 1]; System.arraycopy(original, 0, n, 0, element ); System.arraycopy(original, element+1, n, element, original.length - element-1); return n; } 
0
source

Very simple, use LINQ to find the duplicate in the list.

-3
source

All Articles