leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit dfbe7c919cadefd04c86216ccfaa81d914f4815c
parent 318b2013e3b159d2ecb04d268bc69081ffa243df
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Wed, 12 Apr 2023 16:20:54 -0500

Completed single-number using sort and checking next val

Diffstat:
Msingle-number/a.out | 0
Asingle-number/single-numberV2.cpp | 31+++++++++++++++++++++++++++++++
2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/single-number/a.out b/single-number/a.out Binary files differ. diff --git a/single-number/single-numberV2.cpp b/single-number/single-numberV2.cpp @@ -0,0 +1,31 @@ +#include <iostream> +#include <vector> +#include <unordered_map> +#include <algorithm> +using namespace std; + + +// Time: Memory: +// 27ms 20.2MB +// 35.71% 14.99% +// + +//Another solution that involves sorting the list and then checking to +//see if the next value in the list is equal to the current value. + int singleNumber(vector<int>& nums) { + sort(nums.begin(), nums.end()); + for(int i = 0 ; i < nums.size() - 1 ; i += 2){ + if(nums[i] != nums[i + 1]){ + return nums[i]; + } + } + return nums[nums.size() - 1]; + } + + +int main(){ + //Ans should be 4 + vector <int> vec = {0 , 10, 0, 10, 2, 2, 3, 4, 3}; + cout << singleNumber(vec) << endl; + +}