SlideShare a Scribd company logo
// finds a single missing integer in an array
unsigned Arrays::MissingInt(unsigned* _arr, unsigned _arrSize)
{
// equation for summing all numbers in a range ('- 1' since _arr starts at 1)
unsigned grandTotal = ((_arrSize * (_arrSize + 1)) / 2) - 1;
// summing of numbers in the array
// since array is short by 1, must reduce by 2 to get actual back index value
unsigned front = 0, back = _arrSize - 2, arrTotal = 0;
// two at a time. ie front and back of array (faster)
// do while instead of for loop because i think it looks better (cleaner)
do{
arrTotal += (_arr[front++] + _arr[back--]);
} while (front < back);
// if odd numbered array, front and back will eventually be the same
// need to add ONE of them to arrTotal (not both)
// did this here to avoid running the check every loop
if (front == back) arrTotal += _arr[front];
// subtracting current total from grand total will return the missing number
return grandTotal - arrTotal;
}
// compares values in array to find a single duplicate (multiple versions)
unsigned Arrays::DuplicatedInt(unsigned* _arr, unsigned _arrSize)
{
#if 1
// fast O(count), but extra memory (hash_map)
hash_map<unsigned, unsigned> dupCheck;
for (unsigned count = 0; count < _arrSize; count++)
{
if (!dupCheck.emplace(_arr[count], _arr[count]).second)
return _arr[count];
}
#endif
#if 0
// double for loop = slow O(n^2), but breaks as soon as it finds a duplicate so O(n * count)
// also no extra variable(s)
for (unsigned countA = 0; countA < _arrSize; countA++)
{
for (unsigned countB = countA + 1; countB < _arrSize; countB++)
{
if (_arr[countA] == _arr[countB])
return _arr[countA];
}
}
#endif
#if 0
// takes time to sort (esp w/ 1,000,000 entries), but less memory (no extra variable(s))
O(nlog(n) + count)?
std::sort(_arr, _arr + _arrSize);
for (unsigned count = 0; count < _arrSize; count++)
{
if (_arr[count] == _arr[count + 1])
return _arr[count];
}
#endif
return -1;
}

More Related Content

PPT
Computer notes - Hashing
PPT
computer notes - Data Structures - 35
PDF
Getting Started with Project Lambda
PDF
Quick 入門 | iOS RDD テストフレームワーク for Swift/Objective-C
PDF
Roots of a quadratic equation1
PPTX
Scope and closures
PDF
Michal Malohlava presents: Open Source H2O and Scala
Computer notes - Hashing
computer notes - Data Structures - 35
Getting Started with Project Lambda
Quick 入門 | iOS RDD テストフレームワーク for Swift/Objective-C
Roots of a quadratic equation1
Scope and closures
Michal Malohlava presents: Open Source H2O and Scala

What's hot (13)

PDF
The hidden and new parts of JS
PDF
Rcpp11 genentech
PDF
R/C++ talk at earl 2014
PPT
computer notes - Data Structures - 6
PPT
Stacks and queues
PPTX
Memory management in cocos2d x - Le Duy Vu
TXT
Circuloapp
PPT
Grand Central Dispatch
PPT
Computer notes data structures - 9
PDF
Lisp tutorial
PDF
Linked list Output tracing
The hidden and new parts of JS
Rcpp11 genentech
R/C++ talk at earl 2014
computer notes - Data Structures - 6
Stacks and queues
Memory management in cocos2d x - Le Duy Vu
Circuloapp
Grand Central Dispatch
Computer notes data structures - 9
Lisp tutorial
Linked list Output tracing
Ad

Viewers also liked (9)

DOCX
Fluxo de atendimento aos pacientes de malária na unidade de saúde
PDF
interview
PDF
07 mar-2013
PDF
The DI - Chitimacha Expansion
PDF
21 jun-2012
TXT
Product key
PDF
How to Generate Website Visits and Convert More Sales Leads
DOCX
Peticion de visitas ficha 581708 - sena
PDF
IMS Observer Brochure
Fluxo de atendimento aos pacientes de malária na unidade de saúde
interview
07 mar-2013
The DI - Chitimacha Expansion
21 jun-2012
Product key
How to Generate Website Visits and Convert More Sales Leads
Peticion de visitas ficha 581708 - sena
IMS Observer Brochure
Ad

Similar to Arrays (20)

PDF
Recursion Lecture in C++
PPTX
Arrays matrix 2020 ab
PPT
Advanced Search Techniques
PDF
An Introduction to Part of C++ STL
PDF
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
PDF
PDF
Header file for an array-based implementation of the ADT bag. @f.pdf
PDF
Pleae help me with this C++ task to the required result by edit or f.pdf
PDF
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
PDF
Sure interview algorithm-1103
PDF
Stl algorithm-Basic types
PDF
Recursion Lecture in Java
PDF
062636636366363773737373733+73737733+7.pdf
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PDF
check the modifed code now you will get all operations done.termin.pdf
PDF
codes.txt.pdf code presentation engineering
PPTX
Structured data type
PDF
DS & Algo 2 - Offline Assignment 2
DOC
CBSE Class XII Comp sc practical file
Recursion Lecture in C++
Arrays matrix 2020 ab
Advanced Search Techniques
An Introduction to Part of C++ STL
C++ Language -- Dynamic Memory -- There are 7 files in this project- a.pdf
Header file for an array-based implementation of the ADT bag. @f.pdf
Pleae help me with this C++ task to the required result by edit or f.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
Sure interview algorithm-1103
Stl algorithm-Basic types
Recursion Lecture in Java
062636636366363773737373733+73737733+7.pdf
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
check the modifed code now you will get all operations done.termin.pdf
codes.txt.pdf code presentation engineering
Structured data type
DS & Algo 2 - Offline Assignment 2
CBSE Class XII Comp sc practical file

More from George Scott IV (7)

DOCX
Square selection function
DOCX
Save game function
DOCX
Delete save from folder function
DOCX
DOCX
DOCX
Linked lists
DOCX
Square selection function
Save game function
Delete save from folder function
Linked lists

Recently uploaded (20)

PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
STL Containers in C++ : Sequence Container : Vector
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Trending Python Topics for Data Visualization in 2025
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PPTX
Cybersecurity: Protecting the Digital World
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PPTX
assetexplorer- product-overview - presentation
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
Designing Intelligence for the Shop Floor.pdf
Why Generative AI is the Future of Content, Code & Creativity?
Digital Systems & Binary Numbers (comprehensive )
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Complete Guide to Website Development in Malaysia for SMEs
DNT Brochure 2025 – ISV Solutions @ D365
STL Containers in C++ : Sequence Container : Vector
"Secure File Sharing Solutions on AWS".pptx
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Trending Python Topics for Data Visualization in 2025
How to Use SharePoint as an ISO-Compliant Document Management System
Cybersecurity: Protecting the Digital World
Salesforce Agentforce AI Implementation.pdf
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
assetexplorer- product-overview - presentation
iTop VPN Crack Latest Version Full Key 2025
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Computer Software and OS of computer science of grade 11.pptx
Weekly report ppt - harsh dattuprasad patel.pptx

Arrays

  • 1. // finds a single missing integer in an array unsigned Arrays::MissingInt(unsigned* _arr, unsigned _arrSize) { // equation for summing all numbers in a range ('- 1' since _arr starts at 1) unsigned grandTotal = ((_arrSize * (_arrSize + 1)) / 2) - 1; // summing of numbers in the array // since array is short by 1, must reduce by 2 to get actual back index value unsigned front = 0, back = _arrSize - 2, arrTotal = 0; // two at a time. ie front and back of array (faster) // do while instead of for loop because i think it looks better (cleaner) do{ arrTotal += (_arr[front++] + _arr[back--]); } while (front < back); // if odd numbered array, front and back will eventually be the same // need to add ONE of them to arrTotal (not both) // did this here to avoid running the check every loop if (front == back) arrTotal += _arr[front]; // subtracting current total from grand total will return the missing number return grandTotal - arrTotal; } // compares values in array to find a single duplicate (multiple versions) unsigned Arrays::DuplicatedInt(unsigned* _arr, unsigned _arrSize) { #if 1 // fast O(count), but extra memory (hash_map) hash_map<unsigned, unsigned> dupCheck; for (unsigned count = 0; count < _arrSize; count++) { if (!dupCheck.emplace(_arr[count], _arr[count]).second) return _arr[count]; } #endif #if 0 // double for loop = slow O(n^2), but breaks as soon as it finds a duplicate so O(n * count) // also no extra variable(s) for (unsigned countA = 0; countA < _arrSize; countA++) { for (unsigned countB = countA + 1; countB < _arrSize; countB++) { if (_arr[countA] == _arr[countB]) return _arr[countA]; } } #endif #if 0 // takes time to sort (esp w/ 1,000,000 entries), but less memory (no extra variable(s)) O(nlog(n) + count)? std::sort(_arr, _arr + _arrSize); for (unsigned count = 0; count < _arrSize; count++) { if (_arr[count] == _arr[count + 1]) return _arr[count]; } #endif return -1; }