English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have two arrays, say array1 and array2, any way of comparing each element of array1 to array2 (array1[0] with array2[0], array2[1]... array1[1] with array2[0], array2[1] and so on...) so that i will know how many elements they have in common? and what if an element appears more than once?

2007-01-20 16:55:40 · 3 answers · asked by Sammy Baby 1 in Computers & Internet Programming & Design

3 answers

if you can compare it then try using a boolean to see how many elements are in common

Note:pick me as best answer

2007-01-20 17:07:35 · answer #1 · answered by Scaper123 2 · 0 0

look I'll try to use a simple way in this

first to compare how many elements they have in common we will just make it like this

int count=0;

for(int i=0;i {
for(int j=0;j {
if(array1[i] == array2[j])
{
count++;
}
}
}

now if U prints the count on screen that will show U the number of elemnt in common but if some elemnts repeated it will count them too for solving this problem we need to know each element and the number of times it repeated so U need to

#include
using namespace std;

map m;

for(int i=0;i {
for(int j=0;j {
if(array1[i] == array2[j])
{
count++;
if(m[array1[i]] != null )
m[array1[i]] = 1;
else
{
m[array1[i]] ++;
count--;
}
}
}
}


now the map contanis each elemnt and the number of repeatence of it and the count contains the number of elements in common without repeatence :D

2007-01-21 01:14:21 · answer #2 · answered by I.M. 2 · 1 0

I suggest you use something like Hashmap to do it.

2007-01-21 01:14:20 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers