Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs102/duplicate by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>Given an array of n integers, in which only one number in the array
   has a duplicate, write a program to output the number that is duplicated.</p>
<p>If the user enters array values:</p>
<pre>
4 5 12 -3 1 12 6 7
</pre>
Output:
<pre>
12
</pre>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs102/practice4/apy (author: redekopp@usc.edu)
Copied from problem cpp/cin/average (author: redekopp@usc.edu)
Copied from problem cpp/var-expr/in_n_days (author: redekopp@usc.edu)
Copied from problem cpp/var-expr/char_arith (author: redekopp@usc.edu)
Copied from problem cpp/var-expr/hello (author: daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <cmath>
using namespace std;
int main() {
   const int MAX = 40;
   int data[MAX];
   int n;
   cin >> n;
   
   // Read in n integers
   for(int i=0; i < n; i++){
       cin >> data[i];
   }
   
\[
   for(int i=0; i < n; i++){
      for(int j = i+1; j < n; j++){
         if(data[i] == data[j]){
            cout << data[i] << endl;  
         }
      }
   }
]\
   
      
  return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin":"8 4 5 12 -3 1 12 6 7", "args":[]},
   {"stdin":"2 -3 -3", "args":[]}   
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.