Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [set]

A set is a collection in which no element is repeated, which may be able to enumerate its elements according to an ordering criterion (an "ordered set") or retain no order (an "unordered set").

3
votes
1answer
65 views

Generic Macro Generated Hashset in C

The Hashset uses open addressing, linear probing and Robin Hood hashing for handling collisions. It comes with insert and remove ...
4
votes
2answers
109 views

Custom Hashset in Java

I have written this custom hashset and though it isn't completed yet, I would like to know if there is anything I am overlooking in terms of clean code conventions. My aim was also to apply generics ...
4
votes
1answer
38 views

unordered_set: a hash table -based set data structure in C89

In this post, I present a hash table -based set data structure written in C89: unordered_set.h ...
0
votes
1answer
90 views

java.util.Set extension/implementation

Lately, I was working on some experimental java.util.Set extension, IdSet: ...
1
vote
1answer
32 views

Powerset of collection in Clojure

Similar to this question, here's another implementation. Assume the input is already a set. ...
7
votes
1answer
916 views

Sort int array by frequency then value

Problem Take an array of ints as input and run a special sorting algorithm on it. This algorithm must first sort by int frequency, less frequent first, then sort by value. For example ...
4
votes
2answers
274 views

Data Structures for Counting Duplicates and using std::vector::erase

Problem Dupe detection for a vector of ints. I simply want a count of the unique input characters that occurred at least twice. The goal is to count a dupe only once and ignore that input character ...
1
vote
1answer
77 views

Possible letter combinations of a dial pad in linear time using a recursive approach

Time complexity: I walk the initial unzipped array of keys representing the number O(N) backwards. I then do an inner walk of the proceeding array of letters *O(L^2), mapping it to every value in the ...
1
vote
1answer
941 views

All subsets of a String in java using recursion

I wrote this code for printing all sets of a String. It also prints the empty subset. Is this the right approach ...
0
votes
2answers
488 views

Finding Repeat and missing numbers in an array

Problem Statement: You are given a read-only array of n integers from 1 to n. Each integer appears exactly once except A which appears twice and B which is missing. Return A and B. ...
1
vote
1answer
118 views

Grow-only set (CRDT)

I made up a quick and dirty implementation of the G-Set in C#. I was hoping for some feedback on my approach. I'm not totally happy with having the payload public but I can't see that I'm gaining ...
4
votes
2answers
3k views

From a set of words, output all words that fit a string of random letters (like Scrabble)

I am in my first year of programming with Python 3 and am wondering if anyone had a better way to program this problem. When the user provides up to 7 random letters as a single string, the program ...
2
votes
1answer
397 views

set implementation based on an array

I was supposed to implement set without using any implemented structures. I would love to know what can I correct in my code, because I am not that much satisfied with its quality, so if you have any ...
2
votes
2answers
38 views

Implementation of a shift operation for Set in JavaScript that emulates the one for arrays

I discovered that the JavaScript sets are ordered but don't offer a method to take the first element out of the set. For the arrays this operation exists and it's called shift. A very basic ...
3
votes
3answers
190 views

Finding the number of ways to partition {1,2, …, N} into p1 and p2 such that sum(p1) == sum(p2)

I am trying to write a space and time efficient algorithm to calculate the number of ways to partition a set of integers {1, 2, ..., N} into two partitions such that the sums of integers in the two ...
2
votes
1answer
132 views

Ruby Connected Components in a Graph

I've written the following code to find the connected components of a graph in ruby, but it is quite slow since I don't use any sensible data structure for the disjoint sets, and hence find_set is ...
6
votes
2answers
96 views

is_subset Python implementation

I wanted to ask for a code review for my implementation of Set: ...
1
vote
0answers
72 views

Sorted set a Queue

I needed a data structure offering both poll returning the smallest element and fast contains. A ...
1
vote
0answers
36 views

Benchmarking TreeSet in Java against a van Emde Boas tree based integer set

VanEmdeBoasTreeIntegerSet.java ...
5
votes
1answer
3k views

Concurrent HashSet

I've recently been using HashSet and locking on each method, I found this to not only be a lot of work (was using it in a lot of places) but I started to see inconsistency in my code. I later decided ...
1
vote
3answers
1k views

Finding common Integers between two lists

Write a method numInCommon that takes two Lists of integers as parameters and returns the number of unique integers that occur in both lists. Use one or more Sets as storage to help you solve this ...
2
votes
1answer
103 views

Python implementation for Set

I wanted to ask for a code review for my implementation of Set: ...
3
votes
3answers
82 views

Finding duplicates between n Sets

I have n Objects, Each Object has a Set of "nested" "Neighboring Objects". I need to check if there are duplicates between the "Neighboring Objects" Sets (there are no duplicates inside a single Set ...
3
votes
2answers
2k views
4
votes
1answer
3k views

Compute power set of a given set in Python

Problem: Given a set (let's say of integers), return the list of all of its subsets also known as the power set. It looks to be working, but I would like feedback on the algorithm and Python ...
3
votes
1answer
1k views

Thread safe tbb::concurrent_hash_map and std::set

I have a basic websocket server implemented in C++. I'm using Intel TBB's concurrent hash_map to store all the users, uniquely identified by the id. The concurrent ...
6
votes
1answer
218 views

Finding added keys, removed keys, and unchanged keys in ordereddict

I'm comparing two OrderedDict objects and I need to find added keys, removed keys, and keys that are present in both (the intersection). Sets are designed for ...
3
votes
1answer
89 views

Sorted vector/set class that works with simple types (char, int, double, etc…)

I'm new to C++ (approx. 3 months) and have just read Stroustrup's Programming Principles and Practice. Previously used Python a lot. As an exercise I tried to implement a custom container, with the ...
1
vote
1answer
64 views

Largest divisible subset optimization

So this is my solution to the following leetcode problem: https://leetcode.com/problems/largest-divisible-subset/description/ How could I optimize this code? I think the run time is \$\mathcal{O}(n^2)...
9
votes
4answers
5k views

Powerset (all subsets of a set) in Java

I am trying to get a powerset (all subsets of a set) in Java. So my logic for that is: A given set is one subset, so add so it as it is to result. Remove each element of set from given set and we ...
4
votes
2answers
224 views

Forming sets from pairs of numbers

I do not want to use a new data structure but the existing APIs in Java. Here's a solution. I am thinking if this can be made simpler. Input say ...
0
votes
3answers
302 views

Java Generics and ArrayLists

This abomination of code creates arrays based off input from the user. Sample input would be something like "S 4 aba bab baa aab" where: args[0] = array type (<...
3
votes
1answer
4k views

Set class with binary search tree implementation

Here is my Set implementation with binary search tree that compiles. Please let me know what changes are needed to make it better. Thank you. ...
5
votes
3answers
725 views

Monty hall python simulation

I have the following code that simulates the monty hall problem (see google for more details). I used sets to do it but is there a more intuitive or efficient way to do it? ...
3
votes
2answers
356 views

Set with custom compare

In my project I use std::set with custom Compare function for maintaining an event queue. It fulfills following criteria: event duplication is not allowed should ...
2
votes
2answers
9k views

Finding the intersection of elements in two sets

Is there another better way to implement those two methods in 1 and 2? Implement the method contains. contains returns ...
2
votes
1answer
39 views

A value set data structure for rapid statistics queries in Java - follow-up

(See the previous iteration.) Now all operations of the data structure work in exact constant time, yet it came with the price: the state of this set maybe non-sensical. A user may end up with the ...
2
votes
2answers
68 views

A value set data structure for rapid statistics queries in Java

(See the next iteration.) This question is an elaboration of my question. Just like the previous implementation, all operations (except clear) run in exact or ...
3
votes
1answer
515 views

Extract index of first unique element in large array in Swift

I'm using the following code to return the first index of a unique character in a large String. It works fine until I get to large strings, where it times out. Is ...
3
votes
1answer
7k views

Remove Duplicates from Array in Swift 3.0

I ran the following code through a Leetcode.com challenge and it says the runtime for the test cases is 119ms, which puts it at the back of the pack performance wise. The goal is to remove duplicates ...
2
votes
0answers
94 views

Spellchecker program is having issues due to vowel checking method

This program takes in a list of 275k or so words from '/usr/share/dict/words' and loads them into HashSet. It then takes in a list of 1000 words (taken from the dictionary and are scrambled (vowel ...
2
votes
1answer
321 views

Dictionary with ISet key as a collection of names

I'm writing a command-line utility and I need to find commands (and parameters) by name. The name can either be a full name like save or a shortcut ...
6
votes
2answers
1k views

Building a set of all subsets recursively in C++

Here is my recursive implementation of a power set function in C++. One thing that concerns me is that I am iterating over the set of sets while modifying its size. The code nevertheless functions as ...
5
votes
2answers
2k views

Determining if a relation is reflexive

A binary relation \$R\$ on a set \$A\$ is called reflexive if and only if \$R(a, a)\$ for every element \$a \in A\$. I want to know if there can be any improvements made on the function below to ...
3
votes
0answers
103 views

Substitute for `std::array::operator[]` in C++11 by using template-based recursion

What follows is a simplified version of a tentative implementation for a template-based set, named TemplateArgs. For the sake of clarity and focus on the question ...
2
votes
2answers
177 views

Find combination of sets which sum up to a specified result

Example input : A) 1, 2 B) 2, 8 C) 7, 8 Required Result : 8, 10 The output from the program with this input should be A + C. Few notes : We can only combine 2 sets. There must ...
3
votes
1answer
92 views

Return “support count” for a given itemset after searching a large list of transactions

Not having found a satisfactory association rules implementation in Python, I set out to code my own. The code below runs reasonably quickly, but the groceries dataset used for testing is peanuts ...
0
votes
2answers
859 views

Generate powerset in JS

I came across a mock interview question in which the candidate is asked to generate the powerset of a given set. The input set is represented as a unique array of integers. There was no solution ...
1
vote
1answer
685 views

Printing the power set of a given set of elements

Input: Set of elements (String) Output: Printing the elements of the power set of the given set ...
2
votes
3answers
814 views

Sorting a list by the modulus of 3

The input is an unsorted array of integers. The output should be an array of integers sorted based on the modulus of 3 (or any other number). It begins with all integers conforming to Integer % 3 == ...