Skip to content

Commit

Permalink
Solved contains-duplicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Jun 4, 2023
1 parent 0552fc3 commit ebe769f
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
"${fileDirname}/${fileBasenameNoExtension}",
"-Wall"
],
"options": {
"cwd": "${fileDirname}"
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ include(GoogleTest)

add_subdirectory( two-sum )
add_subdirectory( longest-substring-without-repeating-characters )
add_subdirectory( roman-numerals )
add_subdirectory( roman-numerals )
add_subdirectory( contains-duplicate )
25 changes: 25 additions & 0 deletions contains-duplicate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.14)
project(contains_duplicate)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

enable_testing()

add_executable(
contains_duplicate_runner
runner.cpp
)

add_executable(
contains_duplicate_tests
tests.cpp
)
target_link_libraries(
contains_duplicate_tests
GTest::gtest_main
)
include_directories(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES})

gtest_discover_tests(contains_duplicate_tests)
30 changes: 30 additions & 0 deletions contains-duplicate/runner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <vector>

#include "solution.hpp"

std::ostream& operator<<(std::ostream& os, const std::vector<int>& vector)
{
os << "[";
bool first = true;
for (const int i : vector)
{
if (!first)
os<<",";
os<<i;
first = false;
}

os << "]";

return os;
}

int main()
{
const std::vector<int> input = {1,2,3,1};
Solution s;
std::cout << "\"" << input << "\": " << (s.containsDuplicate(input) ? "true" : "false") << std::endl;

return 1;
}
19 changes: 19 additions & 0 deletions contains-duplicate/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <vector>
#include <unordered_set>

class Solution {
public:
bool containsDuplicate(const std::vector<int>& nums)
{
std::unordered_set<int> seen;
for (int i : nums)
{
if (seen.find(i) != seen.end())
return true;
seen.insert(i);
}
return false;
}
};
34 changes: 34 additions & 0 deletions contains-duplicate/tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <gtest/gtest.h>

#include <utility>
#include <vector>

#include "solution.hpp"

class ContainsDuplicatesTest :public ::testing::TestWithParam<std::pair<std::vector<int>, bool>> {
protected:
Solution solution;
};

TEST_P(ContainsDuplicatesTest, CheckSolution) {
std::vector<int> input = std::get<0>(GetParam());
bool expected = std::get<1>(GetParam());

bool actual = solution.containsDuplicate(input);

EXPECT_EQ(expected, actual);
}

INSTANTIATE_TEST_CASE_P(
ContainsDuplicatesTests,
ContainsDuplicatesTest,
::testing::Values(
std::make_pair<std::vector<int>, bool>({}, false),
std::make_pair<std::vector<int>, bool>({ 1 }, false),
std::make_pair<std::vector<int>, bool>({1, 1}, true),
std::make_pair<std::vector<int>, bool>({1, 2}, false),
std::make_pair<std::vector<int>, bool>({1,2,3,1}, true),
std::make_pair<std::vector<int>, bool>({1,2,3,4}, false),
std::make_pair<std::vector<int>, bool>({1,1,1,3,3,4,3,2,4,2}, true)
)
);

0 comments on commit ebe769f

Please sign in to comment.