• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

C/C++ Puzzles

Andy said:
9. Remove duplicates in an no key access database without using an array

Assuming this is referring to a MS Access database table with no key, a real quick and easy way of doing this is using a copy of the table structure.

You can then use these SQL statements.

C++:
INSERT INTO Table2(Field1)
SELECT DISTINCT Field1 From Table1

DELETE * FROM Table1

INSERT INTO Table1(Field1)
SELECT Field1 FROM Table2
 
Here is a quick function that tests removing duplicates from an array of integers. The question is very vague on what to do with the original array or if the function should produce a new array. As an example, I'm just going to print the duplicates.

This piece of code uses the STL:

C++:
void remove_dups()
{
    int array_size = 6;
    int my_array[] = { 1, 2, 3, 1, 2, 3 } ;

    set<int> int_set;
    for (int i = 0; i < array_size; i++){
        int_set.insert(my_array[i]);
    }
    for(set<int>::iterator it = int_set.begin(); it != int_set.end(); it++){
        cout << (*it) << " ";
    }
    cout << endl;
}
 
Here is a quick function that tests removing duplicates from an array of integers. The question is very vague on what to do with the original array or if the function should produce a new array. As an example, I'm just going to print the duplicates.

This piece of code uses the STL:

C++:
void remove_dups()
{
    int array_size = 6;
    int my_array[] = { 1, 2, 3, 1, 2, 3 } ;

    set<int> int_set;
    for (int i = 0; i < array_size; i++){
        int_set.insert(my_array[i]);
    }
    for(set<int>::iterator it = int_set.begin(); it != int_set.end(); it++){
        cout << (*it) << " ";
    }
    cout << endl;
}

Here is a Java version :) I love java!!
C++:
public static void find_dups(int[] array)
{
    HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>(20,0.75f);
    for(int key: array)
    {
      hash.put(key,(hash.containsValue(key)) ? 1 : 0);
    }

    for(int key : hash.keySet())
    {
      System.out.print(key + " ");            
    }
}
 
In Java, this is a little bit better, my original code to remove duplicates should be like this:

C++:
 public static void removeDuplicates(int[] pArray){
  HashSet<Integer> lSet = new HashSet<Integer>();
 
  for(int i : pArray){
   lSet.add(i);
  }
  for(Integer idx : lSet){
   System.out.print(idx);
  }
 }
 
Here is a quick function that tests removing duplicates from an array of integers. The question is very vague on what to do with the original array or if the function should produce a new array. As an example, I'm just going to print the duplicates.

This piece of code uses the STL:

C++:
void remove_dups()
{
    int array_size = 6;
    int my_array[] = { 1, 2, 3, 1, 2, 3 } ;

    set<int> int_set;
    for (int i = 0; i < array_size; i++){
        int_set.insert(my_array[i]);
    }
    for(set<int>::iterator it = int_set.begin(); it != int_set.end(); it++){
        cout << (*it) << " ";
    }
    cout << endl;
}
Another method, this time using <algorithm>. It doesn't retain the initial order, but it's a simple piece of code:

C++:
void retain_unique (vector<int>& values) {
	sort (values.begin(), values.end());
	vector<int>::iterator u_end = unique (values.begin(), values.end());
	values.erase(u_end, values.end());
}
 
Back
Top