-3

Duplicate of Count occurrences of words in ArrayList

i trying to counts duplicates of my JSON response.

Im got values from 1,2 ... to 13,14

First what i do is sort the list:

Collections.sort(calues);

every value can be used 4 times.

So at last i have list with 1,1,1,1,2,2,2,2.....14,14,14,14

I need to count repeated values and when any value like 1,2 etc will be repeated 3 times i need to give message to user.

How i can do this? Thanks for help :)

Going with tutorial i create this:

 public int count(List<Integer> nums) {
    boolean inclump = false;
    int clumpcnt = 0;
    for (int i = 1; i < nums.size(); i++) {
        if (Objects.equals(nums.get(i), nums.get(i - 1)))  {
            if (!inclump) {
                inclump = true;
                clumpcnt++;
                if(clumpcnt ==3){
                    Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
                }
            }
        } else {
            inclump = false;
        }
    }
    return clumpcnt;
}

but this count the values and when values like 2,2 and 3,3 and 4,4 then this function enter

if(clumpcnt ==3){
                        Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
                    }

I declared a list private List<Integer> cardsValues = new ArrayList<>();

Then i added values here:

first 5 on start application:

 @Override
 public void onNext(@NonNull Cards cards) {
  cardsArray = cards.getArrayCards();
 for (Card item : cardsArray) {
 imgUrls.add(item.getImage());
 cardsValues.add(item.getRank());

} }

And the on every click with "Get new Cards"

Im adding new value with this same method

for (Card item : cardsArray) {
imgUrls.add(item.getImage());
cardsValues.add(item.getRank());
}

And here i call the method

private void checkDuplicateAchievement() {
 Collections.sort(cardsValues);
 count(cardsValues);
}

Here is my full method

private void getCards(final String deck_id, final int count) {
        cardApi.getCards(deck_id, count)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Cards>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Cards cards) {
                        cardsArray = cards.getArrayCards();
                        for (Card item : cardsArray) {
                            imgUrls.add(item.getImage());
                            cardsValues.add(item.getRank());
                        }
                        cardsRecyclerView.notifyDataSetChanged();                    recyclerView.smoothScrollToPosition(cardsRecyclerView.getItemCount());

                        checkDuplicateAchievement();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
Mani
  • 2,599
  • 4
  • 30
  • 49
Rodriquez
  • 981
  • 1
  • 7
  • 21
  • Duplicate of https://stackoverflow.com/questions/5211194/count-occurrences-of-words-in-arraylist – Mani Jul 25 '17 at 04:44

3 Answers3

3

You can make use of a HashMap like below. ** Not tested , typing java after a long time!, Just to give you an idea.

     public int count(List<Integer> nums) {
          Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
          for (int i = 1; i < nums.size(); i++) {
            if(countMap.get(nums[i]) != null){
              countMap.set(i, countMap.get(nums[i]) + 1) 
              if (countMap.get(i) >=3){
                 Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
              }
            }else{
              countMap.set(i, 1)
            }
          }
     }
gipsy
  • 3,859
  • 1
  • 13
  • 21
2

Try this:

public int count(List<Integer> nums) {
    boolean inclump = false; // Not used

    int clumpcnt = 0;
    int value = nums.get(0);
    for (int i = 1; i < nums.size(); i++) {
        if (Objects.equals(value, nums.get(i))) {
            clumpcnt++;
            if (clumpcnt == 3) {
                Utils.showToast(getApplicationContext(), "WOW VALUE REPEATED 3 TIMES, YOU WIN");
                Log.d("COUNT", "WOW VALUE " + String.valueOf(value) + " REPEATED 3 TIMES, YOU WIN");

                clumpcnt = 0;
            }
        } else {
            clumpcnt = 0;
            value = nums.get(i);
        }
    }

    return clumpcnt;
}

FYI, nums list contains 1,1,1,1,2,2,2,2,3,3,3,3

OUTPUT LOG:

28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 1 REPEATED 3 TIMES, YOU WIN
28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 2 REPEATED 3 TIMES, YOU WIN
28078-28078/com.ferdous.stackoverflowanswer D/COUNT: WOW VALUE 3 REPEATED 3 TIMES, YOU WIN
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
1

You can sort your list and then loop the sorted elements and compare values at adjacent positions.If they match then duplicates are present

Vikram Saini
  • 2,713
  • 1
  • 16
  • 33