user971035
user971035

Reputation: 35

How to find max duplicate number count in given array using for loop

input: arr[]={4,3,6,6,4,8,1,9,3,6,7,8,6} output: 6-count (4)

public static int countmaxDuplicate(int arr[]) {
        int maxcount = 0;
        int count = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length - 1; j++) {
                if (arr[i] == arr[j]) {
                    count++;
                }
                if (count > maxcount) {
                    count = maxcount;
                }
            }
        }

        return maxcount;
    }

this code i am trying to implement to get max duplicate count but not able to get solution please suggest me how get output using for loop

Upvotes: 0

Views: 301

Answers (3)

Pham Trung
Pham Trung

Reputation: 11284

There are few issues in your code

        for (int j = i + 1; j < arr.length; j++) {

        }

And

        if (count > maxcount) {
                maxcount = count;
        }

Plus, you need to reinitialise count in every stage.

Full version:

public static int countmaxDuplicate(int arr[]) {
    int maxcount = 0;

    for (int i = 0; i < arr.length - 1; i++) {
        int count = 1;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                count++;
            }

        }
        maxcount = Integer.max(maxcount, count);
    }

    return maxcount;
}

You can also add a boolean array check to avoid duplicating work

public static int countmaxDuplicate(int arr[]) {
    int maxcount = 0;
    boolean[]check = new boolean[arr.length];
    for (int i = 0; i < arr.length - 1; i++) {
        if(check[i]){
           continue;
        }
        int count = 1;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                check[j] = true;
                count++;
            }

        }
        maxcount = Integer.max(maxcount, count);
    }

    return maxcount;
}

Or even consider to use a HahsMap so this will have O(n) time complexity

HashMap<Integer, Integer> count = new HashMap();
for(int i : arr){
   count.put(i, count.getOrDefault(count, 0) + 1);
}

int maxCount;
for(int i : count.values()){
   maxCount = Integer.max(maxCount, i);
}
return maxCount;

Upvotes: 4

Shadov
Shadov

Reputation: 5592

Just gonna post it here, I don't like for loops.

int[] arr = {4, 3, 6, 6, 4, 8, 1, 9, 3, 6, 7, 8, 6};

Map<Integer, Long> map = Arrays.stream(arr)
        .boxed()
        .collect(Collectors.groupingBy(
                Function.identity(), Collectors.counting()
        ));

Map.Entry<Integer, Long> max = Collections.max(
        map.entrySet(), Map.Entry.comparingByValue()
);

System.out.println(max.getKey());

First you group them all by their count (maybe you need more than element with max count in the future? who knows), then you select max entry and print the key from that.

Upvotes: 1

Kaushal28
Kaushal28

Reputation: 5557

In inner for loop, you are not inspecting last element every time. So change the for loop as:

 for (int j = i + 1; j < arr.length ; j++) {
    //your logic
 }

Also other mistakes are you've to initialize count to 1 initially and also you need to reset count. Also your assignment is not right. You are maintaining maxcount as your final answer, so you need to update it instead of count. So following will work perfectly.

public static int countmaxDuplicate(int arr[]) {
    int maxcount = 1;
    for (int i = 0; i < arr.length - 1; i++) {
        int count = 1;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[i] == arr[j]) {
                count++;
            }

        }
        if (count > maxcount) {
                 maxcount= count;
        }
    }

    return maxcount;
}

Upvotes: 2

Related Questions