helpppp pls!! How to move duplicate value into seperate vectors? | Sololearn: Learn to code for FREE!

+2

helpppp pls!! How to move duplicate value into seperate vectors?

Hi! I am trying to get a lst of numbers and store them into a vector and find the first set of numbers that are identical and move them to a different vector. How do I accomplish that? code: https://code.sololearn.com/cG5D9n73nmM5

c++

5/14/2022 7:47:01 PM

Annei ❤️

13 Answers

New Answer

+3

void group(vector<int>& list) { int product = list[0], max = 0; //set to defaults //start from index = 1 , as already checked first value into product for (int index = 1; index <(int)list.size(); index++) { if( list[index] == list[index-1]) // checking with prevoius product *= list[index]; else // reset product product = list[index]; // first maximum between max, current product if(max < product) max = product; } cout << max ; } //Annei ❤️ try this function. hope it helps..

+1

Ah. So, input is assumed to be sorted? The largest set, in terms of number of elements, would be the set of threes, wouldn't it. So, the answer would be 27, not 64? Is it necessary to group them into vectors first? Because so far they don't appear to be necessary.

+1

There were no 6 in that list 🤔 Three 3, one 7, and two 8. And "most likely" is not "they are" 😄 So, maybe we better sort them first.

+1

Inside your for loop and if statement you have is the following : Ex list = {2,5,5,6,6} for(int index : list) // first loop index = 2; (2 != 1 ) continue //second loop index = 5 (5 ! = 4 ) contiue And so on Instead use: int i = 0; for ( int j = 1 ;j < list.size();j++){ if (list[i] == list[j] ) same.push(list[j]) i++; } And what is it you want with the product ?

+1

You don't need new vectors. Read vector 3 3 3 7 8 8 9 , Max=0 3*3*3 =>27 , Max < 27 so Max = 27 7 => Max < 7 false, unchange 8*8 => 64 , and Max < 64 so change Max = 64. 9 => 9 , Max < 9 false Print Max. Sequentially read vector, On different value than previous, change Max, current Multiplier. you can do these by if -else blocks I think simplysimply, in loop reading sequentially vector Annei ❤️ your way index == index -1 never true. it's like Ex: 3 == 2 you don't get indexes there, you get values to index variable. Hope it helps..

+1

Annei ❤️ I given the algorithm. Did you tried anything or not understood anything?

+1

If order does not matter ex input : 3 3 3 7 8 8 same as : 7 3 8 3 8 3 Heres one way https://code.sololearn.com/ciy3plaM1e2R/?ref=app

+1

THANK YOU!!!!!

0

Can you give an example (or many) of what you wish to accomplish?

0

If it's not necessary, then we can remove that - i think? and yes! They're most likely to be sorted @Ani Jona 🕊 Because 8 > 3, we would ignore 3 and look at the vector that holds the set of 8, 8.

0

This looks about right! However, how would i implement this in code? @Jayakrishna🇮🇳

0

If order matters https://code.sololearn.com/c4jRtBJCeugN/?ref=app

0