Dear all,

I'm a newbie learning Java. Could you please help me on how to find the length of the longest group of duplicates in an array using Java.

Let's say we have an array of integers as follows
{9 7 7 7 7 3 2 1 1 5 5 5 5 5 4 1 3 3 3 9 7 7 3 5 }

The answer is 4 as the group of 5s has 4 duplicates
while the group of 7s has 3 duplicates, the group of 3s has only 2. The "duplicate" here is defined as an integer which has the same value of the integer in the n-1th position.

I'd greatly appreciate your help on this problem.
VM

Loop through the array, saving the "last" value, as well as the currently "longest" duplicate (both what it is and how many there were, depending on what your output needs to be) and increment a "current duplicate" counter. Then, when the value changes, or the array ends, compare that counter with the "current longest" and make any adjusments you need to make.

Loop through the array, saving the "last" value, as well as the currently "longest" duplicate (both what it is and how many there were, depending on what your output needs to be) and increment a "current duplicate" counter. Then, when the value changes, or the array ends, compare that counter with the "current longest" and make any adjusments you need to make.

Hi masijade,

Thanks very much for your guide.
Should I use regular for/while loops or hash table to trace this,
which one you think will work better for this problem?

Thanks,
VM

No need for a map/table of any kind. It's a simple straight for loop run through the array and a few variables and a couple of if statements.

No need for a map/table of any kind. It's a simple straight for loop run through the array and a few variables and a couple of if statements.

Hi masijade,
I'm a little bit confused with longest dup counter and current dup counter.
How do I keep the longest counter and reset the current dup counter when the value changes? Could you please give me an example code.?
Truyly appreciate,
VM

why don't u show some code?

I will write some pseudo code for you

int numCount = 1;
loop over the array from index 1
if i and i-1 entry of array are same
increase numCOunt by 1
else
set Numcount to 1;

done!
aint that simple?

writing this code will take me 1 minute but I want you to code and learn yourself!

oh wait...

int numCount = 1;
int finalCount = 1;
loop over the array from index 1
if i and i-1 entry of array are same
increase numCOunt by 1
else
If numcount is greater than finalcount
set finalcount to numcount
set numCount to 1;
else numcount is 1


sorry :)

Hi masijade,
I'm a little bit confused with longest dup counter and current dup counter.
How do I keep the longest counter and reset the current dup counter when the value changes? Could you please give me an example code.?
Truyly appreciate,
VM

With two different variables, of course. That is why they are referred to with two different names.

commented: Great idea! +1

Hi solahere and masijade,

Oh, Sorry, I've been off from the web for a while.
I think I get it.
Thank you so much for shedding the light upon this.

Have a great day!
VM

Can you tag this thread as solved?

Can you tag this thread as solved?

Hi,
How can I tag it as solved?
Thanks
VM

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.