How to find common elements in two arrays if else are repeated
Answers
Answer:
Find common elements in three sorted arrays
Given three arrays sorted in non-decreasing order, print all common elements in these arrays.
Examples:
ar1[] = {1, 5, 10, 20, 40, 80}
ar2[] = {6, 7, 20, 80, 100}
ar3[] = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20, 80
ar1[] = {1, 5, 5}
ar2[] = {3, 4, 5, 5, 10}
ar3[] = {5, 5, 10, 20}
Output: 5, 5
Explanation:
I’ve declared a type similar to the following.
type
TLikes = record
Name : string[20];
favColours : array of string[20];
faves = array of TLikes;
Once the records are populated I save them to a binary file so the structure is like that shown below.
[John], [Green] [White] [Blue]
[Paul], [Blue] [Red] [White] [Green]
[David], [Red] [Blue] [Green]
[Bob], [White] [Blue]
[Peter], [Blue] [Green] [Red]
It’s easy to find out what colours David, for example, likes. A small problem occurs when I want the to know who likes blue. So what I’ve done is build a second file, like so …
[Blue], [John] [Paul] [David] [Peter] [Bob]
[Red], [David] [Paul] [Peter]
[White], [Bob] [David] [John] [Paul]
[Green], [John] [David] [Paul] [Peter]
But something is telling me, I shouldn’t really need to create a second file / data structure, it just seems inefficient.
Here’s a bigger issue ….
What if I need to find who likes any combination of what David likes? My results would be …
Blue and red and green = Paul, David, Peter
Blue and red = Paul, David, Peter
Blue and green = John, Paul, David, Peter
Red and Green = Paul, David, Peter
My question is.
Is there a better way to structure the data / records so I can figure out what Bob and Paul have in common (Blue and White) or what red and white have in common (David and Paul) ?
I guess I need to point out that I have tried to simplify the example above. In reality the data for Tlikes.Name will be strings like …
‘decabbadc’
‘bacddbcad’
‘eebadeaac’
There are something in the order of 200k+ of these strings. And the Tlikes.FavColours data is a filename (there are around 2k of these files). The file name indicates a file that contains the Tlikes.Name string.
I want to be able to retrieve a list of file names given a Tlikes.Name string or a list of strings given a file name.
NB – Something is drawing me to ‘sets’ but from the little I understand, I’m limited in the number of elements in sets, am I on the right track ?
Thank you for taking the time to read the