Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 174

# Matching numbers in an array

what would be the most simple way to get the highest matching number in an array?
array1[1,2,3,4,6,8,12,14,21,24]
array2[1,2,3,4,5,6,7,8,10,12,15,16]
highest common number is 12.Can you make a SET from an array and us the in function? What is the most simple way to get the answer.
0
Lfteye
• 2
1 Solution

Commented:
depends what u wont
if u wont speed that is a one question
and if u wont simle coding that is difrent question

the simple coding
array1 is a aray with   count1 members;
array2 is a aray with   count2 members;
m,mx is the same type as array1 and array2

var i,j:integer;

begin
m:=0;
for i:=1 to count1 do
begin
mx=m;
for j:=1 to count2 do
begin
if array1[i]=array2[j] then mx=array[i];
end;
end;
if mx>m then m=mx
end;

0

Commented:
If the arrays are sorted, may be something like this is faster to find the highest common number (search from the highest number backwards)

var i, j, count1, count2: integer;
found: Boolean;

found := False;
i := count1;
begin
j := count2;
begin
if array1[i]=array2[j] then
begin
showMessage('Highest common number is '+
intToStr(array1[i]));
found := True;
end
else if array2[j] < array1[i] then
break;
j := j-1;
end;
i := i-1;
end;

hope that helps
0

Commented:
If the arrays are sorted, may be something like this is faster to find the highest common number (search from the highest number backwards)

var i, j, count1, count2: integer;
found: Boolean;

found := False;
i := count1;
begin
j := count2;
begin
if array1[i]=array2[j] then
begin
showMessage('Highest common number is '+
intToStr(array1[i]));
found := True;
end
else if array2[j] < array1[i] then
break;
j := j-1;
end;
i := i-1;
end;

hope that helps
0

Author Commented:
This is the practice program I was fooling with.

program ReduceFraction(output);

var

num1,num2,count,ctr1,ctr2,gcd,i,j : Integer;
Key : Char;
numerator :array[1..30] of integer;
denominator :array[1..30] of integer;
begin
Key:= 'y';
while Key = 'y' do
begin
writeln('enter numerator :');
writeln('enter denominator :');
{--------------------------Numerator--------------------------------------}
ctr1 := 0;
for count := 1 to num1 do
if num1 MOD count = 0 then
begin
ctr1 := ctr1 +1;
numerator[ctr1] := count;    {---assign values to the array---}
end;
{-----------------------Denominator---------------------------------------}
ctr2 := 0;
for count := 1 to num2 do
if num2 MOD count = 0 then
begin
ctr2 := ctr2 +1;
denominator[ctr2] := count;  {---assign values to the array---}
end;
{--------------------------------Find GCD-------------------------------------------}
for i := 1 to ctr1 do
begin
for j := 1 to ctr2 do
begin
if numerator[i] = denominator[j] then
gcd := denominator[j]
end;
end ;
{-------------------------Print reduced fraction---------------------------------------------}
writeln(num1,'/',num2,' reduced is ',num1 div gcd,'/',num2 div gcd,' the GCD is ',gcd);
writeln('continue (y)es/(n)o');