You are here:

C++/C++ Searching algorithm


I have a file with many rows and coloumns.Suppose the first column is A,then how do i find out whether the first value of A is duplicated anywhere in the file or not?  

Your basic approach is to treat the file as an array. For the problem as stated it is relatively academic whether you retain the 2 dimensionality of the data or treat it as a single dimensional dataset. The reason for this observation is that for this problem the data is most easily treated as a vector, i.e. a single dimensional array.

Assume that the data is read into an vector of values, one value in the vector for each value in the dataset contained in the file. You then iterate through the vector (iterate through: loop through) of values from 1 to n 1 where n is the number of items in the dataset.

Remember that in C and C++ array subscripts start at 0, so the first item in the vector has a subscript of zero. Hence you check each item from  subscript 1 to n - 1 to the item of subscript zero (the first item) to see if any have the same value. If so then yes there are duplicates and the first value is not unique.

You can use the standard library algorithm find to help with the details of this operation:

      T * pItem = std::find( &array[1], &array[n], array[0] );

Where T * the type of pointers to items in the array and n is the number of items. pItem is assigned the result of the find operation and is either a pointer to the first item that matches the item array[0] or the pointer to array[n]. Hence if the first item value (array[0]) is unique the pItems will be &array[n].  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.