user1339752
user1339752

Reputation:

Delete duplicate strings in string array

I am making a program based on string processing in Java in which I need to remove duplicate strings from a string array. In this program, the size of all strings are same.

The 'array' which is a string array contains a number of strings in which two strings resemble each other. So using the below code the duplicate string must get removed but it is not removed.

How to remove the duplicate strings?

I am using the following code.

    for(int s=0;s<array.length-1;s++)
    {
        for(int m=0;m<array.length;m++)
        {
                for(int n=0;n<array[m].length();n++)
                {   
                    if(array[s].charAt(n)==array[m].charAt(n))
                    {
                      continue;
                    }
                    else 
                break;
        } 
        if(n==array[m].length())
        {
            ArrayUtils.removeElement(array, array[s]);
        }
    }

Upvotes: 24

Views: 109152

Answers (15)

Hunter
Hunter

Reputation: 1

To remove duplicate String from String[] without using Collection

    public static void removeDuplicate(String[] str, int size){
            for(int i=0; i<size-1; i++){
                if(str[i]!=null){
                    for(int j=i+1; j<size-1; j++){
                        if(str[i].equals(str[j])){
                            str[j]=null;
                        }
                    }
                }
            }
            for(int i=0; i<size;i++){
                if(str[i]==null)
                   continue;
                System.out.println(str[i]);
            }
        }

Upvotes: 0

Alexey Konovalov
Alexey Konovalov

Reputation: 30

public static List<String> sortHandleArrayList(String... arrayInput) {
    List<String> list = new ArrayList<>();
    for (String string : arrayInput) {
        if (!list.contains(string)) {
            list.add(string);
        }
    }
    Collections.sort(list);
    return list;
}

Upvotes: 0

Afif Khaja
Afif Khaja

Reputation: 1

Sring[] myStringArray = {"hello", "hello", "moto"};
String[] filteredArray = new LinkedHashSet<String>(Arrays.asList(myStringArray))
                         .toArray(new String[0]);

System.out.println("filteredArray Size: " + filteredArray.length);
System.out.println("filteredArray[0] = " + filteredArray[0]);
System.out.println("filteredArray[1] = " + filteredArray[1]);

Upvotes: 0

Traycho Ivanov
Traycho Ivanov

Reputation: 3207

Set data structure will do automatically the job for. Most likely option for you will be HashSet, if you care about the order of elements look at TreeSet

List<String> input = Arrays.asList(array);
Set<String> unique = new HashSet<>(input);

Upvotes: 1

ekishore4u
ekishore4u

Reputation: 1

List<String> al = new ArrayList<String>();
String[] months={"Jan","Feb","Mar","Apr","Jan","Mar","May","May"};
for(int i=0;i<months.length;i++){
    for(int j=1;j<months.length;j++){
        if(months[i].equalsIgnoreCase(months[j])){
            if(!al.contains(months[i])){
                al.add(months[i]);
            }
        }
    }
}

Upvotes: 0

Thangenapally Rajesh
Thangenapally Rajesh

Reputation: 21

import java.util.*;
public class Stringarray {

    public static void main(String args[]){

        String[] name = {"aim","rajesh","raju","aim"};

    Set<String> myset  = new HashSet<String>();
    Collections.addAll(myset,name);

       System.out.println(myset);
    }
}

Upvotes: 2

Hubert Schumacher
Hubert Schumacher

Reputation: 1985

Proposed solution does not keep the order of the elements. If you use Java 8 or higher and want to maintain the order you can use streams as follows:

array = Arrays.stream(array).distinct().toArray(String[]::new);

Full example: https://www.javacodeexamples.com/java-string-array-remove-duplicates-example/849

Upvotes: 9

Janmejai
Janmejai

Reputation: 1

     String[] arr = {"w10","w20","w10","w30","w20","w40","w50","w50"};
     List<String> arrList = new ArrayList<String>();
     int cnt= 0;
       //List<String> arrList = Arrays.asList(arr);
       List<String> lenList = new ArrayList<String>();
          for(int i=0;i<arr.length;i++){
        for(int j=i+1;j<arr.length;j++){
           if(arr[i].equals(arr[j])){
             cnt+=1;
           }                
        }
        if(cnt<1){
          arrList.add(arr[i]);
        }
          cnt=0;
        }

for(int k=0;k<arrList.size();k++){
            System.out.println("Array without Duplicates: "+arrList.get(k));
        }

Upvotes: 0

haris
haris

Reputation: 1

Duplicate integer remove : this is the perfect answer /// Haris ///

public static void duplicateRemove(int[] arr) {
    int temp = 0;

    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr.length; j++) {
            if (arr[i] < arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    int count;
    for (int j = 0; j < arr.length;) {
        count = 1;
        for (int i = j + 1; i < arr.length; i++) {
            if (arr[i] == arr[j]) {
                count++;
            } else
                break;

        }
        System.out.println(arr[j] + " is :  " + count);
        j += count;
    }

}

Upvotes: -3

Martin Prakash
Martin Prakash

Reputation: 66

I think the if condition at the end should be if(n==(array[m].length()-1))

Having said that, you seem to be trying to implement what String.equals() method does in your inner most loop.

Upvotes: 0

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7302

Why didn't you remove the most inner loop with in favor of String.equals(String)?

In first iteration you are comparing array[0] with array[0] which are equal, and it would be removed. Then you will compare the original array[1] with all other elements in array, and if they are equal, you are removing array[1] (not the other one).

There are some problems, if there are some duplicate Strings, you are removing the first one, which will reduce the size of the array without reducing r so, some of the Strings in the array are skipped.

I would use a data structure which forces uniqueness, such as a Set.

What will happen if you have 3 equal Strings in your array, I'm not sure what will happen.

I believe you would encounter some ArrayIndexOutOfBoundsExceptions.

Upvotes: 0

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116266

  • Why don't you use String.equals() for comparison instead of iterating through the characters in the strings manually?
  • Your logic is actually flawed: for array[s] == "12345" and array[m] == "123" it would claim that they are equal
  • moreover, in your inner loop for(int m=0;m<array.length;m++) m will also become equal to s at some point, so you will compare a string to itself

These notes assume that you need to implement the removal logic with your own code, not being allowed to use the class library. If this is not the case, as others noted, using a HashSet is the simplest approach.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533520

Unless this is [homework] I would use a Set

String[] array =
Set<String> uniqueWords = new HashSet<String>(Arrays.asList(array));

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109557

Set<String> set = new HashSet<String>();
Collections.addAll(set, array);

or start with

for(int s=0;s<array.length-1;s++)
{
    for(int m=s + 1;m<array.length;m++)
    {

                if(array[s] != null && array[s].equals(array[m]))
                {
                  // array = ArrayUtils.removeElement(array, array[s]); --m;??
                  array[m] = null; // Mark for deletion later on
                }
    } 
}

Upvotes: 2

Garrett Hall
Garrett Hall

Reputation: 30032

This will work

array = new HashSet<String>(Arrays.asList(array)).toArray(new String[0]);

or just use a HashSet instead of an array.

Upvotes: 46

Related Questions