How to Find Max Duplicate Number Count in Given Array Using for Loop

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

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;

Java - Find maximum number of duplicates within an array

This is the Element Distinctness Problem - which is explained with details in the thread: Find duplicates in an array.

The mentiones thread discusses solutions to the problem, and shows lower bounds as well (cannot be done better than O(nlogn) without using a hash table.

So, if your data is not sorted - you could sort and iterate (as follows), or use a hash set - and then you don't need to sort the array.

If you first sort the array, or the array is already sorted, a single iteration will do:

Single iteration on a sorted array:

if (arr == null || arr.length == 0) return 0;
int last = arr[0];
int numDupes = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == last) numDupes++;
last = arr[i];
}

Using a HashSet (no need to sort):

if (arr == null) return 0;
Set<Integer> set = new HashSet<>();
int numDupes = 0;
for (int x : arr) {
if (set.contains(x)) numDupes++;
set.add(x);
}

If you are looking for the maximal number some element repeats (and not total number of repeats), you can use the same approach but slightly different:

Hashing solution - use a histogram:

Map<Integer,Integer> histogram = new HashMap<>();
for (int x : arr) {
if (!histogram.containsKey(x)) histogram.put(x,1);
else histogram.put(x,histogram.get(x) + 1);
}
int max = 0;
for (int x : histogram.values) max = max > x ? max : x;
return max;

Sorted array solution:

if (arr == null || arr.length == 0) return 0;
int last = arr[0];
int max = 0;
int currNumDupes = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == last) currNumDupes++;
else {
max = max > currNumDupes ? max : currNumDupes;
currNumDupes = 1;
}
last = arr[i];
}
max = max > currNumDupes ? max : currNumDupes; //if the most dupes is from the highest element

How to count duplicate value in an array in javascript

function count() {    array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
array_elements.sort();
var current = null; var cnt = 0; for (var i = 0; i < array_elements.length; i++) { if (array_elements[i] != current) { if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times<br>'); } current = array_elements[i]; cnt = 1; } else { cnt++; } } if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times'); }
}
count();

How to count maximum duplicate value in an array in javascript

You could do it e.g. with this code:

max = 0;
$.each(arr, function(){
current_max = 0;
current = this;
$.each(arr, function(index, value){
if(current==value){
current_max += 1;
}
});
if (current_max > max){
max = current_max;
}
});

https://jsfiddle.net/aec8nhvj/

Find duplicate in Array with single loop

Use any Set implementation, say HashSet<T>, e.g.

HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };

foreach (item in Arr)
if (hs.Contains(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}
else
hs.Add(item);

Edit: since hs.Add returns bool a shorter and more efficient code can be put:

HashSet<int> hs = new HashSet<int>();
int[] Arr = { 9, 5, 6, 3, 8, 2, 5, 1, 7, 4 };

foreach (item in Arr)
if (!hs.Add(item)) {
Console.WriteLine("duplicate found");
// break; // <- uncomment this if you want one message only
}

count of duplicate elements in an array in php

You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.

array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);

Plus, it's a one-liner, thus adding to elite hacker status.

Update

Since 5.5 you can shorten it to:

array_count_values(array_column($arr, 'lid'));


Related Topics



Leave a reply



Submit