So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
Help first. Criticize second.
Joshua Harris wrote:Hello,
So, as the title says I'm trying to count the number of elements in an ArrayList which also have duplicates. So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
Help first. Criticize second.
Kat Rollo wrote:@Liutauras Vilda:
Why? Was his answer incorrect?
Help first. Criticize second.
So, don't be annoyed, but I have pulled rank and deleted the post (it remains in memory so I might restore it later).We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Joshua Harris wrote:So, as the title says I'm trying to count the number of elements in an ArrayList which also have duplicates. So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
If an element is found to be a duplicate, that element should then be exempt from the search so if that element is found again it should not increase the duplicate count.
Campbell Ritchie wrote:The reason is that handing out a complete answer prevents OP learning from the exercise. This is what it says on the contents page for this forum:
So, don't be annoyed, but I have pulled rank and deleted the post (it remains in memory so I might restore it later).We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Tapas Chand wrote:Before going for the solution please check your if condition. There is a semicolon after the if condition, that means it is not doing anything.
The statements after the if condition will always be executed no matter what are the elements of the list.
And there is no braces for the if condition.
You should always have braces for if/else/while/for as a good coding practice.
Kat Rollo wrote:
So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
For example if your list contains:
cat
cat
dog
dog
horse
zebra
zebra
The output is 3 because cat, dog, and zebra are duplicated?
Junilu Lacar wrote:How would you do this by hand, without the help of a computer?
Here's another hint: histogram
Guillermo Ishi wrote:The j = i+1 is a great way of avoiding comparing twice, which should be in the back of everybody's mind.
Junilu Lacar wrote:Besides the coding error mentioned by Tapas Chand, check if your algorithm will work with a list like this: {cat, dog, cat, dog, cow, dog, duck, duck, goose} - note that 'dog' appears three times in the list.
Joshua Harris wrote:I was thinking of creating a new ArrayList and filling it with elements which are known to be duplicates, then maybe making another if statement which checks if the element which is found to be a duplicate is already within that new ArrayList, and then not bothering to increase the duplicate count if it is (I hope that makes sense, I'm pretty terrible at explaining things). But I just thought there must be a more simple way which I'm just not thinking of.
Joshua Harris wrote:
Kat Rollo wrote:
So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
For example if your list contains:
cat
cat
dog
dog
horse
zebra
zebra
The output is 3 because cat, dog, and zebra are duplicated?
Yes. No matter how many duplicates there are of a word, it should only increase the duplicate count by 1. It's counting how many words there are which have duplicates, not how many duplicates of a word there are.
Help first. Criticize second.
Campbell Ritchie wrote:Well done
![]()
Please show us how.
Carey Brown wrote:
Guillermo Ishi wrote:The j = i+1 is a great way of avoiding comparing twice, which should be in the back of everybody's mind.
What about: { cat, cat, horse, dog, cat, cat, zebra } ?
Campbell Ritchie wrote:You're lucky the test site is not pickier. I have restored the solution which I deleted this morning, so you can compare the two.
Yes.Joshua Harris wrote: . . .
Is that the one written by Karthick Pattabiraman? . . .
Carey Brown wrote:My solution (eluded to earlier)
;
Help first. Criticize second.
Kat Rollo wrote:Here's my solution with comments and sysouts so you see what's happening.
Help first. Criticize second.
Joshua Harris wrote:How can I make it stop doing this?
Kat Rollo wrote:It's also intended to be nearer the OP's original code:
Don't get me started about those stupid light bulbs. |