Ha KiM's
Ha KiM's

Reputation: 121

remove repeated words from String Array

Good Morning

I write a function that calculates for me the frequency of a term:

public static int tfCalculator(String[] totalterms, String termToCheck) {
    int count = 0;  //to count the overall occurrence of the term termToCheck
    for (String s : totalterms) {
        if (s.equalsIgnoreCase(termToCheck)) {
            count++; 
        }
    } 
    return count;
}

and after that I use it on the code below to calculate every word from a String[] words

for(String word:words){
    int freq = tfCalculator(words, word);

    System.out.println(word + "|" + freq);
    mm+=word + "|" + freq+"\n";
}

well the problem that I have is that the words repeat here is for example the result:

so can someone help me to remove the repeated word and get as result like that:

Thank you very much!

Upvotes: 1

Views: 1664

Answers (6)

shmosel
shmosel

Reputation: 50716

Your code is fine, you just need keep track of which words were encountered already. For that you can keep a running set:

Set<String> prevWords = new HashSet<>();
for(String word:words){
    // proceed if word is new to the set, otherwise skip
    if (prevWords.add(word)) {
        int freq = tfCalculator(words, word);

        System.out.println(word + "|" + freq);
        mm+=word + "|" + freq+"\n";
    }
}

Upvotes: 0

Moad MEFTAH
Moad MEFTAH

Reputation: 251

in two line :


String s = "cytoskeletal|2 - network|1 - enable|1 - equal|1 - spindle|1 - cytoskeletal|2"; System.out.println(new LinkedHashSet(Arrays.asList(s.split("-"))).toString().replaceAll("(^\[|\]$)", "").replace(", ", "- "));

Upvotes: 0

Saurav
Saurav

Reputation: 51

I think here you want to print the frequency of each string in the array totalterms . I think using Map is a easier solution as in the single traversal of the array it will store the frequency of all the strings Check the following implementation.

public static void printFrequency(String[] totalterms)
{
    Map frequencyMap = new HashMap<String, Integer>();

    for (String string : totalterms) {
        if(frequencyMap.containsKey(string))
        {
            Integer count = (Integer)frequencyMap.get(string);
            frequencyMap.put(string, count+1);
        }
        else
        {
            frequencyMap.put(string, 1);
        }
    }

    Set <Entry<String, Integer>> elements= frequencyMap.entrySet();

    for (Entry<String, Integer> entry : elements) {
        System.out.println(entry.getKey()+"|"+entry.getValue());
    }
}

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140309

Sort the array, then you can just count equal adjacent elements:

Arrays.sort(totalterms);
int i = 0;
while (i < totalterms.length) {
  int start = i;
  while (i < totalterms.length && totalterms[i].equals(totalterms[start])) {
    ++i;
  }
  System.out.println(totalterms[start] + "|" + (i - start));
}

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

Java 8 solution

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

the distinct method removes duplicates. words is replaced with a new array without duplicates

Upvotes: 2

Idos
Idos

Reputation: 15310

You can just use a HashSet and that should take care of the duplicates issue:

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

This will take your array, convert it to a List, feed that to the constructor of HashSet<String>, and then convert it back to an array for you.

Upvotes: 0

Related Questions