Click here to Skip to main content
15,881,620 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a required which should satisfy all three conditions below

1. one or more 2 ---- keep only one 2, drop rest

Ex : 2,2,2,2 - > 2
2. one or more 3----keep only one 3, drop rest

Ex: 3,3,3,3 -> 3

3. one or more 2 + one or more 3 .....keep only 2, drop the rest

Ex : 2,2,3,3,3,3 -> 2.

Please help me in writing c # code which satisfies all above conditions.

Thank you.

What I have tried:

if (count of 2) > 1
retain first 2 and delete rest
if(count of 3) > 1
retain first 3 and delete rest

3rd condition?
Posted
Updated 31-May-16 3:38am
Comments
Sergey Alexandrovich Kryukov 31-May-16 9:26am    
Please read carefully:
What have you tried so far?
Instead, you just repeated what is said in your requirements.
—SA
PIEBALDconsult 31-May-16 9:28am    
It's _your_ homework; _you_ do it.

if (count of 2) > 0
retain first 2 and delete rest
if(count of 3) > 0
retain first 3 and delete rest
if (count of 2 > 0 && count of 3 > 0)
retain first 2 and delete rest
 
Share this answer
 
Comments
ZurdoDev 31-May-16 9:44am    
The condition you added should be the first condition that is checked.
F-ES Sitecore 31-May-16 10:23am    
TBH as it stands the third check isn't needed at all, it is covered by the first one. The issue is if you want to ignore things that aren't in the count *shrug*
The question is not correctly formulated, the formulation is not started from the beginning.

Here is the idea: if input is an array, you need to understand that you cannot delete any elements from the array after it is initialized. Therefore, you would have to create a brand new array object and return it. For each case, you count how many array elements you need in an output array and initialize a new array with this length. Then you copy elements from the input array to the output array, but, depending on the case, not all of them.

—SA
 
Share this answer
 
Comments
George Jonsson 31-May-16 16:39pm    
Not sure why you got a downvote on this one.
+5
Sergey Alexandrovich Kryukov 31-May-16 21:14pm    
Thank you, George.

These days, just one member of high reputation down-vote each and every of my answers (or almost so), no matter what they are, by big blocks.

I don't think this member even reads them, because all votes in a block (typically 4-8 answers at a time, sometimes more) come withing the same minute. I call it "hate voting pattern", nothing else... It looks like someone wants to never hear anything from me. As if the votes could do it...

At the same time, this voting mangles the impression from the answers, because the readers who cannot judge on the quality of the answers may be mislead into thinking that something is wrong with a particular answer they read at the moment...

—SA
George Jonsson 31-May-16 22:22pm    
Your welcome.
Seems like someone has written a little "downvote-all-Sergeys-answers" bot.
I have had a few unexplained downvotes myself.
But there are also a lot of downvotes for the questions. Sure, some questions are not all that well phrased or shows a bit of lazyness, but it will eventually scare people off so there will be no more questions.
Sergey Alexandrovich Kryukov 31-May-16 22:44pm    
A bot? Never though of that. I believe it's done manually.
You see, unexplained down-votes are very usual, it can be fine when they are done in individual manner, not massive, based on understanding of the subject. Frankly, there are many posts, both questions and answers, where the authors don't pay any real effort, just fool around or hope for luck; such posts hardly deserve explanations...
—SA
George Jonsson 31-May-16 23:08pm    
If it is done manually, it has to be a fellow with lightning speed mouse skills and a determination from h**l, coz the doer has to scan through quite a lot of questions in order to find yours.
Alas, there are quite a few "gimmie code" questions that can strain your patience.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900