Discrete Mathematics and Its Applications, Seventh Edition

Published by McGraw-Hill Education
ISBN 10: 0073383090
ISBN 13: 978-0-07338-309-5

Chapter 3 - Section 3.1 - Algorithms - Exercises - Page 203: 31

Answer

procedure repeated $(a_1, a_2, ... a_n: $ a sequence of $n$ integers $)$ for $i:= 2$ to $n$ $\space\space\space$ for $k:=1$ to $i$ $\space\space\space$$\space\space\space$ if $a_i$ is equal to $a_k$ then return $a_i$ return undefined {Since the question does not specify what to return in case no match is made - we cannot choose to return 0 or -1 since an integer can take these values; however, an integer may not be undefined which indicates to a user that an error has occurred}

Work Step by Step

This algorithm attempts to find the first integer value that is equal to a preceding term. The solution is a brute-force type of solution since it just compares each value to each value before it and checks if they are the same. There are more elegant solutions but those require techniques that are beyond a discrete mathematics course (An example for example would be efficiently storing a list of the values that we've already found and then referencing the new value to see if it matches any previous result.)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.