Home > Software design >  Text to string array and delete duplicates
Text to string array and delete duplicates

Time:12-16

The idea of the programme is that it gets the text divided by space from the scanner. Need to write a method that creates an array from text and delete duplicates and return an array of the words which are used only once and don't have duplicates. I can't find how to make a new array of unique words. Using only simple and basic construction without Hash,Set etc.

For example : a b a b c a b d

result: c d

 public static String Dublicate(String text) { 
  String[] dublic = text.split(" ");
  String result="";
  for (int i =0;i<dublic.length; i  ){
    for(int j =i 1;j<dublic.length; j  )
      if(dublic[i].equals(dublic[j]))
       dublic[j] = "delete";
  }
  for(String s: dublic){
    if(s !="delete"){
      result =result   s   " ";  
    }
  }
  return result;
  }

CodePudding user response:

Split By Space

For splitting by space we can use the split() method & can pass the Space string ("") in the parameter.

String[] texts = text.split(" ");

Delete The duplicate elements

If We can use java 1.8 or greater than 1.8, we can use stream API for getting distinct elements like.

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

Or if we need to implement it with java 1.7, we can use HashSet for getting distinct elements like.

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

The final Source code can be like this:

public static String[] textToArray1_7(String text) {
        //split by space
        String[] texts = text.split(" ");
        //Distinct value
        return Arrays.stream(texts).distinct().toArray(String[]::new);
    }

    public static String[] textToArray1_8(String text) {
        //split by space
        String[] texts = text.split(" ");
        //Distinct value
        return  new HashSet<String>(Arrays.asList(texts)).toArray(new String[0]);
    }

If any further question, can ask for more clarification.

CodePudding user response:

You forgot to mark i-th element as duplicate in case when it really is. See my comments in the below code

public static String Dublicate(String text) {
    String[] dublic = text.split(" ");
    String result="";
    for (int i=0; i<dublic.length; i  ){
        if (dublic[i].equals("delete")) { // Minor optimization: 
            // skip elements that are already marked
            continue;
        }
        boolean isDub = false;   // we need to track i-th element
        for(int j=i 1; j<dublic.length; j  ) {
            if (dublic[i].equals(dublic[j])) {
                dublic[j] = "delete";
                isDub = true;   // i-th element is also a duplicate...
            }
        }
        if (isDub) {
            dublic[i] = "delete";  // ...so you should also mark it
        }
    }
    for(String s: dublic){
        if(!s.equals("delete")) { // for strings you should use "!equals" instead of "!="
            result = result   s   " ";
        }
    }
    return result;
}

P.S. if original text contains "delete" the result will be incorrect since you use "delete" as a reserved marker word

  • Related