0
ArrayList<String> ArrayTemp = new ArrayList<>();
for(int x=0;x<ABC.size();x++){
            for(int y=0;y<ArrayTemp.size()+1;y++){
                if(!ABC.get(x).equals(ArrayTemp.get(y))){
                   ArrayTemp.add(ABC.get(x));
                   CountTemp.add(Count[x]);
                }
            }
        }

Above shown my code, I am going to compare each element for the array ABC to remove duplicate of the element. Meanwhile I need the Count value also. Basically the contain and result will look like: Contain:

ABC Count
aaa   9
aaa   9
aab   8
aba   8
aaa   9
aab   8

The result i need is:

ArrayTemp    CountTemp
aaa              9
aab              8
aba              8

Besides, i tried to use

Set ArrayTemp = new HashSet(); Set CountTemp = new HashSet();

This code seem does not synchronize my contain. Can anyone help me to solve this problem? Does't matter which the code is using, as long this can help me. Feel free to answer me and i will try each of the coding provided. Thank you very much.

3 Answers 3

1

Take a look at Map in Java. That's what you are looking. put unique String as a key and put count as value.

Eg:

List<String> ArrayTemp = new ArrayList<>();
Map<String, Integer> countMap = new HashMap<>();
for (String key : ArrayTemp) {
   Integer count = countMap.get(key);
   if(count==null){
     countMap.put(key,1);
    }else {
     countMap.put(key,count+1);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might help:

Map<String, Integer> countMap = new HashMap<>();
for(int x=0; x < ABC.size(); x++){
  countMap.put(ABC.get(x), Count[x]);
}

If you want to put aggregated count you can try something like (assuming you have the Count already populated):

Map<String, Integer> countMap = new HashMap<>();
for(int x=0; x < ABC.size(); x++){
  if (countMap.containsKey(ABC.get(x))) {
     countMap.put(ABC.get(x), countMap.get(ABC.get(x)) + Count[x]);
  } else {
     countMap.put(ABC.get(x), Count[x]);
  }
}

Ofcourse if you want the whole thing to start by using map (word count) - here is the link.

Comments

0

Why do not you go for quick solutions rather than re-inventing the wheel. You can also use apache commons collections to have duplicates removed from your Lists:

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>


  ArrayList<String> duplicatedList =new ArrayList<String>(0);
  duplicatedList.addAll(Arrays.asList("aaa","aaa","bbb","ccc","aab","aaa","cca","ccc","bbb") );

  SetUniqueList uniqueList = SetUniqueList.decorate(duplicatedList); 
  System.out.println(uniqueList.toString());

One line solution by Apache Commons Collections ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.