I have two arrays of multiple strings and I want to print first array elements which are only present in it and not in the other | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have two arrays of multiple strings and I want to print first array elements which are only present in it and not in the other

i.e if some elements of first array are common with the second array then they should not be printed

22nd Aug 2018, 3:47 AM
Kanchan
8 Answers
+ 9
Kanchan Here, a working C++ code solution. Enjoy! 😁👍🏻 https://code.sololearn.com/c3qGjpB8fYYF/?ref=app
22nd Aug 2018, 4:05 AM
Eduardo Petry
Eduardo Petry - avatar
+ 6
# Python arr1 = ["potato", "doggo", "cheerios", "barbecue", "cream", "lavander", "ice"] arr2 = ["doggo", "dinosaur", "cream", "robot", "potato", "sandwich"] for word in arr1: if word not in arr2: print(word)
22nd Aug 2018, 3:53 AM
Eduardo Petry
Eduardo Petry - avatar
+ 6
Kanchan Well, to be fair, I am not that good in C++, but from what I see you created a class for students and they play Cricket/Badminton. Is your code compiling when you run it on your IDE? You should first guarantee that your current version is compiling and with no critical bugs before you try adding a new feature. You need to determine which students play only Cricket (and not Badminton), right? My suggestion is that you take a look at the code I made, run it a couple times and figure out how it is working. Then implement it in your own code as a separate function (so that it can be taken out if it happens to bug the rest of your code, but I do believe it is working well). Then use the function to determine which students play only Cricket! Good luck and feel free to ask questions!
22nd Aug 2018, 5:11 AM
Eduardo Petry
Eduardo Petry - avatar
+ 1
https://code.sololearn.com/czk4qo4iToPW/?ref=app This is my code. Can you tell me what is wrong in this code?
22nd Aug 2018, 5:00 AM
Kanchan
+ 1
Eduardo Petry Yes my code is compiling and giving output same as the input array. I'll try to figure out how your code works. Thank you for your efforts.
22nd Aug 2018, 5:41 AM
Kanchan
+ 1
Error is on line 52: if(name[i] == name1[j]). What you mention here is probably comparing two words from multidimensional array of chars, but you must notice i.e. name[0] is an array, so compiler converts it to a pointer to the first element of this array -> you compare here memory adresses, not words. Moreover even if you have here arrays of chars insted of pointers, comparison is still invalid becouse c++ lacks of operation comparing two arrays of chars (C - style strings); you must use library functions for such tasks, for example strcmp() from <cstring>. I strongly recommend using std::string class which is modern solution for such kind of problems. Feel free to ask if something is not clear.
22nd Aug 2018, 6:13 AM
Jakub
0
My question is for c++. Sorry I didn't mention.
22nd Aug 2018, 3:55 AM
Kanchan
0
It is not working. It prints all the elements which are present in first array.(It prints the input)
22nd Aug 2018, 4:26 AM
Kanchan