Skip to content

Commit 786d74f

Browse files
committed
completed 217 - contains duplicate in go, py, rust and c++
1 parent 36f57a5 commit 786d74f

File tree

18 files changed

+559
-0
lines changed

18 files changed

+559
-0
lines changed

cpp/contains_duplicate/.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# General
2+
*.log
3+
*.tmp
4+
5+
# IntelliJ IDEA Files
6+
.idea/
7+
*.iml
8+
*.iws
9+
*.ipr
10+
11+
# CLion files
12+
cmake-build-debug/
13+
**/cmake-build-debug/
14+
**/build/
15+
compile_commands.json
16+
17+
18+
# Eclipse Files
19+
.classpath
20+
.project
21+
.settings/
22+
23+
# NetBeans Files
24+
nbproject/private/
25+
build/
26+
nbbuild/
27+
dist/
28+
nbdist/
29+
.nb-gradle/
30+
31+
# VS Code
32+
.vscode/
33+
34+
# Compiled Go binaries
35+
/bin/
36+
/pkg/
37+
38+
# Output of the go coverage tool
39+
*.out
40+
41+
# Python
42+
__pycache__/
43+
/__pycache__/
44+
*.pyc
45+
venv/
46+
.env
47+
48+
# Rust
49+
/target/
50+
target/
51+
**/target/
52+
**/*.rs.bk
53+
Cargo.lock
54+
55+
# Windows image file caches
56+
Thumbs.db
57+
ehthumbs.db
58+
59+
# Folder config file
60+
Desktop.ini
61+
62+
# Recycle Bin used on file shares
63+
$RECYCLE.BIN/
64+
65+
# Mac desktop service store files
66+
.DS_Store

cpp/contains_duplicate/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(lc_cpp_template)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
8+
# Include FetchContent module
9+
include(FetchContent)
10+
11+
# Try to find Google Test
12+
find_package(GTest QUIET)
13+
14+
# If Google Test is not found, download it
15+
if(NOT GTest_FOUND)
16+
message(STATUS "Google Test not found. Downloading...")
17+
FetchContent_Declare(
18+
googletest
19+
GIT_REPOSITORY https://github.com/google/googletest.git
20+
GIT_TAG release-1.12.1 # Specify a version/tag here
21+
)
22+
# For Windows: Prevent overriding the parent project's compiler/linker settings
23+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
24+
FetchContent_MakeAvailable(googletest)
25+
endif()
26+
27+
# Include directories
28+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
29+
30+
# Add the solution library
31+
add_library(solution
32+
src/solution.cpp
33+
include/solution.h
34+
)
35+
target_include_directories(solution PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
36+
37+
# Enable testing
38+
enable_testing()
39+
40+
# Add the test executable
41+
add_executable(test_solution
42+
test/test_solution.cpp
43+
)
44+
45+
# Link the test executable with Google Test and the solution library
46+
if(GTest_FOUND)
47+
target_link_libraries(test_solution PRIVATE GTest::gtest GTest::gtest_main solution)
48+
else()
49+
target_link_libraries(test_solution PRIVATE gtest gtest_main solution)
50+
endif()
51+
52+
# Add the test
53+
add_test(NAME SolutionTest COMMAND test_solution)
54+
55+
# CTest integration
56+
include(GoogleTest)
57+
gtest_discover_tests(test_solution)

cpp/contains_duplicate/LICENSE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Jeremy Novak All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6+
7+
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
9+
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+
11+
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cpp/contains_duplicate/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# DSA C++ Template
2+
3+
This repo is for quickly setting up a C++ project that uses [CMake](https://cmake.org) and [GoogleTest](https://google.github.io/googletest/).
4+
5+
It is intended for practice with the C++ language while doing Data Structures and
6+
Algorithms problems like those found on [leetcode.com](https://leetcode.com)
7+
8+
## Instructions
9+
10+
1 - Clone the repo to a directory name of your choice.
11+
12+
```bash
13+
git clone https://github.com/jgndev/dsa_cpp_templ example_problem
14+
```
15+
16+
2 - Run the project init script `project-init.sh`
17+
18+
```bash
19+
./project-init.sh
20+
```
21+
22+
This creates a `build` directory and links `compile-commands.json` to the top level which
23+
helps LSPs work in editors like [neovim](https://neovim.io).
24+
25+
## Building
26+
27+
1 - Change directory into `build`
28+
29+
2 - Run `make`
30+
31+
32+
## Running tests
33+
34+
1 - Add your tests to `tests/test_solution.cpp`
35+
36+
2 - Change directory to `build`
37+
38+
3 - Run `make` to compile `test_solution`
39+
40+
4 - Run `./test_solution` to run the tests.
41+
42+
## 217. Contains Duplicate
43+
44+
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
45+
46+
47+
48+
Example 1:
49+
50+
Input: nums = [1,2,3,1]
51+
52+
Output: true
53+
54+
Explanation:
55+
56+
The element 1 occurs at the indices 0 and 3.
57+
58+
Example 2:
59+
60+
Input: nums = [1,2,3,4]
61+
62+
Output: false
63+
64+
Explanation:
65+
66+
All elements are distinct.
67+
68+
Example 3:
69+
70+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
71+
72+
Output: true
73+
74+
75+
76+
Constraints:
77+
78+
1 <= nums.length <= 105
79+
-109 <= nums[i] <= 109
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <unordered_set>
5+
6+
class Solution {
7+
public:
8+
bool containsDuplicate(std::vector<int>& nums);
9+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
IFS=$'\n\t'
5+
6+
# Constants
7+
readonly SCRIPT_NAME="${0##*/}"
8+
readonly BUILD_DIR="build"
9+
10+
# Color codes for output
11+
readonly RED='\033[0;31m'
12+
readonly GREEN='\033[0;32m'
13+
readonly NC='\033[0m' # No Color
14+
15+
# Function to print error messages
16+
print_error() {
17+
printf "${RED}Error: %s${NC}\n" "$1" >&2
18+
}
19+
20+
# Function to print success messages
21+
print_success() {
22+
printf "${GREEN}Success: %s${NC}\n" "$1"
23+
}
24+
25+
# Function to check if a command exists
26+
command_exists() {
27+
command -v "$1" &>/dev/null
28+
}
29+
30+
# Main function
31+
main() {
32+
# Check if CMake is installed
33+
if ! command_exists cmake; then
34+
print_error "CMake is not installed. Please install CMake and try again."
35+
exit 1
36+
fi
37+
38+
# Create build directory if it doesn't exist
39+
mkdir -p "${BUILD_DIR}"
40+
41+
# Generate build files and compile_commands.json
42+
echo "Generating build files and compile_commands.json..."
43+
if ! cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -B "${BUILD_DIR}" .; then
44+
print_error "Failed to generate build files."
45+
exit 1
46+
fi
47+
print_success "Build files and compile_commands.json generated."
48+
49+
# Build the project
50+
echo "Building the project..."
51+
if ! cmake --build "${BUILD_DIR}"; then
52+
print_error "Failed to build the project."
53+
exit 1
54+
fi
55+
print_success "Project built successfully."
56+
57+
# Symlink compile_commands.json to project root
58+
echo "Creating symlink for compile_commands.json..."
59+
if [[ -L compile_commands.json ]]; then
60+
rm compile_commands.json
61+
elif [[ -e compile_commands.json ]]; then
62+
print_error "A file named compile_commands.json already exists and is not a symlink."
63+
exit 1
64+
fi
65+
if ! ln -s "${BUILD_DIR}/compile_commands.json" .; then
66+
print_error "Failed to create symlink for compile_commands.json."
67+
exit 1
68+
fi
69+
print_success "Symlink for compile_commands.json created in project root."
70+
71+
echo "All tasks completed successfully!"
72+
echo "You can now use compile_commands.json for your LSP."
73+
}
74+
75+
# Execute main function
76+
main "$@"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "solution.h"
2+
3+
bool Solution::containsDuplicate(std::vector<int> &nums) {
4+
std::unordered_set<int> seen;
5+
6+
for (int num: nums) {
7+
if (seen.count(num)) {
8+
return true;
9+
}
10+
11+
seen.insert(num);
12+
}
13+
14+
return false;
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <gtest/gtest.h>
2+
#include "solution.h"
3+
4+
class SolutionTest : public ::testing::Test {
5+
protected:
6+
Solution solution;
7+
};
8+
9+
TEST_F(SolutionTest, TestCaseOne) {
10+
std::vector<int> nums = {1, 2, 3, 1};
11+
constexpr bool expected = true;
12+
const bool result = solution.containsDuplicate(nums);
13+
EXPECT_EQ(result, expected);
14+
}
15+
16+
TEST_F(SolutionTest, TestCaseTwo) {
17+
std::vector<int> nums = {1, 2, 3, 4};
18+
constexpr bool expected = false;
19+
const bool result = solution.containsDuplicate(nums);
20+
EXPECT_EQ(result, expected);
21+
}
22+
23+
TEST_F(SolutionTest, TestCaseThree) {
24+
std::vector<int> nums = {1, 1, 1, 3, 3, 4, 3, 2, 4, 2};
25+
constexpr bool expected = true;
26+
const bool result = solution.containsDuplicate(nums);
27+
EXPECT_EQ(result, expected);
28+
}

go/countDuplicates/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 217. Contains Duplicate
2+
3+
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
4+
5+
## Example 1:
6+
7+
Input: nums = [1,2,3,1]
8+
9+
Output: true
10+
11+
Explanation:
12+
13+
The element 1 occurs at the indices 0 and 3.
14+
15+
## Example 2:
16+
17+
Input: nums = [1,2,3,4]
18+
19+
Output: false
20+
21+
Explanation:
22+
23+
All elements are distinct.
24+
25+
## Example 3:
26+
27+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
28+
29+
Output: true
30+
31+
32+
33+
## Constraints:
34+
35+
1 <= nums.length <= 105
36+
-109 <= nums[i] <= 109

go/countDuplicates/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module countDuplicates
2+
3+
go 1.23.2

0 commit comments

Comments
 (0)