in Education by
This is an exercise for the CS guys to shine with the theory. Imagine you have 2 containers with elements. Folders, URLs, Files, Strings, it really doesn't matter. What is AN algorithm to calculate the added and the removed? Notice: If there are many ways to solve this problem, please post one per answer so it can be analysed and voted up. Edit: All the answers solve the matter with 4 containers. Is it possible to use only the initial 2? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Assuming you have two lists of unique items, and the ordering doesn't matter, you can think of them both as sets rather than lists If you think of a venn diagram, with list A as one circle and list B as the other, then the intersection of these two is the constant pool. Remove all the elements in this intersection from both A and B, and and anything left in A has been deleted, whilst anything left in B has been added. So, iterate through A looking for each item in B. If you find it, remove it from both A and B Then A is a list of things that were deleted, and B is a list of things that were added I think... [edit] Ok, with the new "only 2 container" restriction, the same still holds: foreach( A ) { if( eleA NOT IN B ) { DELETED } } foreach( B ) { if( eleB NOT IN A ) { ADDED } } Then you aren't constructing a new list, or destroying your old ones...but it will take longer as with the previous example, you could just loop over the shorter list and remove the elements from the longer. Here you need to do both lists An I'd argue my first solution didn't use 4 containers, it just destroyed two ;-)

Related questions

0 votes
    I have created a fiddle to explain what I want : https://jsfiddle.net/silentway/aro5kq7u/3/ The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    E.x. listA = [9,10,11] listB = [12,13,14] Expected outcome: >>> concatenated list [9,10,11,12,13,14] Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    How will you compare two lists in Python?...
asked Nov 26, 2020 in Technology by JackTerrance
0 votes
    Joining two lists can be achieved either using the _________ function. (a) Concat (b) Join (c) Reduce (d) ... and Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Imagine that you have: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] What is the simplest way ... 42, 'food' : 'spam'} Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I have two lists in Python, like these: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One' ... ways without cycles and checking? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want to create a new list from the existing two lists that will have all the matches from list 2 which has an exact ... *", i) in a] Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I am having two lists list1 = ['Apple', 'Orange', 'Grapes', 'Banana'] list2 = ['Apple', 'Orange'] I want to ... = ['Grapes', 'Banana'] Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have a list of files in a folder in my system file_list= ["A", "B", "C"] I Have read the files using ... C How do I accomplish this? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    Into which two categories can the waste materials in the lists above be classified? Select the ... Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    What are lists and tuples? What is the key difference between the two?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What are the differences between t.test() and t.data.frame()? When do you use these two functions?...
asked Oct 16, 2020 in Technology by JackTerrance
0 votes
    How can we see the differences between the two branches in Git?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    Which of the following lists names of variables in a data.frame? (a) quantile() (b) names() (c) ... Linear Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
...