hi all ,
i have two arrays say :

``````a[] = {"abc" , "def" , "ghi"};
b[] = {"abc" , "def" };
``````

now i would like to find out the difference i.e "ghi" ..without using nested for loops.

Edited by happygeek: fixed formatting

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span

how about using a function (Warning! not compiled or tested.)

``````bool IsInArray(const char array[], int size, const char* search)
{
bool found = true;
for(int i = 0; i < size; i++)
{
if( strcmp( array[i], search) == 0)
{
found = true;
break;
}
}
return found;
}

int main()
{
char* a[] = {"abc", "def", "ghi"};
char* b[] = {"abc", "def"};

for(int i = 0; i < 3; i++)
{
if( IsInArray(b, 2, a[i] )
cout << a[i] << "\n";
}
}``````

how is this different from using nested for loops .. it has the same complexity as that of nested for loops ..... the idea in not using nested for loops is to reduce the time complexity ....

how about using a function (Warning! not compiled or tested.)

``````bool IsInArray(const char array[], int size, const char* search)
{
bool found = true;
for(int i = 0; i < size; i++)
{
if( strcmp( array[i], search) == 0)
{
found = true;
break;
}
}
return found;
}

int main()
{
char* a[] = {"abc", "def", "ghi"};
char* b[] = {"abc", "def"};

for(int i = 0; i < 3; i++)
{
if( IsInArray(b, 2, a[i] )
cout << a[i] << "\n";
}
}``````

Honestly, you're not giving us enough information.

This is what I managed to come up with, based on many assumptions--

``````#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::size_t;

// determines the difference between two sets
// this function assumes that one set is the subset of the other
// and that the elements in each set are ordered.
// the elements of each set consist of 3-char strings in sequential order
const char** difference(const char**, size_t, const char**, size_t);

int main(){

const char* a[] = {"abc" , "def" , "ghi"};
const char* b[] = {"abc" , "def" };
const char** d = difference(a, 3, b, 2);

cout << *d << endl; // as an example, though more values in d could exist

cin.get();
return 0;
}

const char** difference(const char** first, size_t s1,
const char** second, size_t s2){

const char** result = 0;

// the location of difference
size_t differingPosition = 0;

// determining the difference in sizes - which set is the set with more
// values?
size_t minSize = (s1 < s2) ? s1 : s2;

/*
* Here we're simply iterating through all of the elements of each array
* attempting to locate the point where things start to change.
*
* I'm also making the assumption that each string consists of 3 characters,
* hence the modulo 3.
*/
for(size_t i = 0; first[i][i%3] == second[i][i%3] && i < minSize * 3; i++)
differingPosition = i + 1;

// if no point of change was located, we shouldn't be returning anything
// else other than a null set of values, otherwise return the remainder set
if(differingPosition > 0 && ( s1 != s2 ))
result = (s1 > s2) ? &first[differingPosition] : &second[differingPosition];

return result;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.