flutter
flutter

Reputation: 6766

Count the number of ListArrays when anyone of the individual ListArrays has duplicate elements

I need a method that checks many different ArrayLists. For each individual ListArray I need to check for duplicity(any instance of two elements the same) and when the condition is met (ie two elements are the same in an ArrayList) I need to count the amount of times the condition is met and the int value returned after all the ListArrays have been checked. Here is my code so far to give a better idea. It's not complete. I just need guidance with the return type int and how to increase the count when there is duplicity of elements in an individual ListArray

private static int findDuplicates(List<Integer> list) {

 int total=0;

 for (int j=0;j<list.size();j++)
   for (int k=j+1;k<list.size();k++)
     if (k!=j && list.get(k).equals(list.get(j)));
 total=+ total;

return total;
}

Upvotes: 0

Views: 58

Answers (2)

Ousmane D.
Ousmane D.

Reputation: 56413

Syntax and logical problems

1) the ; at the end of the if condition basically prevents the if statement to do anything at all, in other words it's equivalent to an if statement with empty body.

2) You're incorrectly incrementing the total variable.

3) ensure you include the { } braces.

4) you're using the wrong logic to increment total=+ total;, it should be total += total;

you need to increment by 1;

total += 1;
//or if you prefer
total++;

not by 0:

total += total;

Example:

private static int findDuplicates(List<Integer> list) {

 int total=0;

 for (int j = 0; j < list.size(); j++){
     for (int k = j + 1; k < list.size(); k++){
        if (k != j && list.get(k).equals(list.get(j))){
            total++;
        }
     }
 }

 return total;
}

Problem within your algorithm

Also, your algorithm appears to have a problem because let's say if an element appears few/several times in the list, it will count it more than once. if that's what you intended then no problem. However, if that's not the case then you can try the approach below.

The approach below basically ignores all the duplicates that have been processed already when encountered later on within the list and proceeds.

private static int findDuplicates(List<Integer> list) {

    int total=0;
    List<Integer> tempList = new ArrayList<>();

    for (int j = 0; j < list.size(); j++){
        for (int k = j + 1; k < list.size(); k++){
             if (k != j && list.get(k).equals(list.get(j)) && !tempList.contains(list.get(k))){
                 tempList.add(list.get(k));
                 total++;
             }
        }
    }

    return total;
}

Expanding on the alternative approach.

let's assume this is the list which we want to pass to the findDuplicates method.

List<Integer> list = Arrays.asList(
        1,2,3,3,3,3,3,4,5,6,7,8,9,5,6,7,13,13,13,15,16
);

System.out.println(findDuplicates(list));

with the approach I have provided it will print 5, However, with your approach, it will print 16.

Upvotes: 0

Sibgha
Sibgha

Reputation: 489

There seems to be a problem with the algorithm too. If an element appears three times in the list, it will count it twice. If it appears four times, it will be counted thrice.

Upvotes: 1

Related Questions