Java - Find maximum number of duplicates within an array -


i utilizing hashset finding maximum number of duplicates of value in sorted integer array. algorithm doesn't seem work, not returning desired results.

set variables storing number of duplicates found (0), , maximum number of duplicates (0).  set hashset stores unique values of array. sort array ready comparison.  loop through each value of array     if hashset of unique values contains current value:         increment duplicate count      if currentvalue not equal previous value:         if duplicatecount greater maximum count:             maximumcount becomes duplicatecount             reset duplicatecount 0 

java code:

hashset<integer> uniquevalues = new hashset<integer>(valuesequencelist);  int duplicatecount = 0; int maxcount = 0; arrays.sort(valuesequence);  (int = 0; < valuesequence.length; i++) {     if (uniquevalues.contains(valuesequence[i]))     {         duplicatecount++;     }     if (i > 0 && valuesequence[i] != valuesequence[i-1])     {         if (duplicatecount > maxcount)         {             maxcount = duplicatecount;             duplicatecount = 0;         }     } } 

example:
input: [4, 4, 10, 4, 10]
output: 4 duplicates (there supposed maximum of 3 duplicates - total number of values same).

this element distinctness problem - explained details in thread: find duplicates in array.

the mentiones thread discusses solutions problem, , shows lower bounds (cannot done better o(nlogn) without using hash table.

so, if data not sorted - sort , iterate (as follows), or use hash set - , don't need sort array.

if first sort array, or array sorted, single iteration do:

single iteration on sorted array:

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

using hashset (no need sort):

if (arr == null) return 0; set<integer> set = new hashset<>(); int numdupes = 0; (int x : arr) {      if (set.contains(x)) numdupes++;     set.add(x); } 

if looking maximal number element repeats (and not total number of repeats), can use same approach different:

hashing solution - use histogram:

map<integer,integer> histogram = new hashmap<>(); (int x : arr) {    if (!histogram.containskey(x)) histogram.put(x,1);    else histogram.put(x,histogram.get(x) + 1); } int max = 0; (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; (int = 1; < 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 dupes highest element 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -