# Tag Info

Accepted

### Partitioning an array based on a condition in Javascript

Using TypeScript/ECMAScript 6 syntax it can be achieved this way. I am not sure whether it's more or less elegant compared to the original variant, but It does the job; Requires only one run; Can be ...
• 3,490
Accepted

### Zero-initializing large dynamically allocated arrays of double

So my first hunch when I read your question is: Okay this guy is making a complicated solution to a simple problem based on a hunch that the trivial solution is "grossly inefficient" ...
• 16.6k

### Partitioning an array based on a condition in Javascript

Anything wrong with the naive ways? ...
• 396
Accepted

### Prime sieve for all primes up to a number

Comments Most people write bad comments. Things that should be commented are idea/decisions/why. Writing comments that explain the code is horrible. This is because comments often fall out of date ...
• 95.2k

### Finding the majority element in an array

Boyer-Moore for the curious You have a good answer already suggesting the use of a Counter. For simplicity, that's what I would do. However, I had never heard of the Boyer-Moore algorithm for finding ...
• 12.7k
Accepted

### Determine whether to push or update object in array based on unique ID

Your pushToArray function is less efficient than it could be because it it looping over the entire array multiple times. We could instead use find()/findIndex() to ...
• 2,732
Accepted

### Finding the minimum number of moves to achieve equal array elements

The problem can be re-formulated into a much easier-to-conceptualize form. Instead of Given a non-empty integer array of size n, find the minimum number of moves required to make all array ...
• 14.9k
Accepted

### Finding the first duplicate in an array

For a small array or list of a handful of items, this should be very fast. But is it efficient as you would scale out? No. Before getting on to a better way to achieve the it for a larger number of ...
• 6,350

### Create two random teams from a list of players

Review Don't use the main entry point to implement an algorithm. Create a method instead. Think about how to allow this method to be usable for all kinds of types, not just integers. You have ...
• 13.9k
Accepted

### Determine if an array is the reverse of a second array

The first comment, is that it would be preferable to factor this into a dedicated method, like areReversed(int[] array1, int[] array2). It would clarify the ...
• 9,243

### Cropping a message using array splits

This is much slower than necessary. It takes time to construct the array, and more to shorten the array word-by-word. It's easy to imagine how this would go if ...
• 4,205

### Return first duplicate

I'll propose an alternate implementation, because dictionaries are good at storing key-value pairs and you don't care about the value: ...
• 62.1k
Accepted

### Dynamic array implementation using C++

First of all, there is a major problem with your class: it does not respect the rule of three/five/zero: it has a non trivial destructor that destroys memory allocated in constructor it neither ...
Accepted

### Algorithm for dividing a number into largest "power of two" buckets?

CertainPerformance's solution using .toString(2) is clever, but for a fast solution to such an elementary problem, simple bit manipulation and a ...
• 3,039
Accepted

### First non-repeating Character, with a single loop in Python

Python has a Counter data structure that is handy whenever you are ... counting things. Since strings are directly iterable, you can easily get a dict-like tally ...
• 12.7k

### Partitioning an array based on a condition in Javascript

The existing answers do not seem to address the callback. One way to include it could be: ...
• 900
Accepted

### Check if three arrays contains the same element

Your code assumes that each of the 3 arrays is sorted. Otherwise the < operator would not work. It's ok to assume this. You should have mentioned this in your ...
• 21.3k

### Count the frequency of items in an array

Answer to the Performance Issue Anytime there are a list of variables named NAMEi where i is an integer, there is a strong chance that a container such as std::array or std::vector should be used. ...
• 23.8k

### Implementation of a two-dimensional array in C

You are doing too many allocations. The usual way of implementing multidimensional array is to allocate one continuous buffer and map n-dimensional coordinate to a 1-dimensional one. You then provide ...
• 5,477
Accepted

### Rotate an array to the right by a given number of steps

In place solution Jon Bentley "Programming Pearls" (1986) has this task as one of problems. The recommended solution, instead of either: doing K 1-element rotations, using 1 temporary variable, ...
Accepted

### Bit array implementation

Don't write a function to return a constant You don't need uint_size(), as the returned value never changes (for a given target platform). You can replace it with ...
• 75.7k
Accepted

### Array duplicate removal for duplicates exceeding N-number

As you expected, there is a much simpler way to do this. Create a map where the keys are the numbers in the array and the value is the number of times each number appears in the array. Filter the ...
• 3,456
Accepted

### Checking for parentheses balance in C

Bugs The loop header is wrong: for(i=0;line[i]!=NULL;i++){ Here, you want to scan until you find a byte with ASCII value 0 (also called an ASCII NUL, and ...
• 144k

### Finding the first duplicate in an array

Elaborating on my rather cryptic comment. There is no better hash for an integer than an integer itself, which of course degenerates the HashMap into an array. The ...
• 56.5k
Accepted

### Calculate min, max, mean and median out of array

I see a number of things that may help you improve your program. Don't violate const The calculate_median routine takes a <...
• 66.4k

### Find if one list is a subsequence of another

Review Testing (I haven't rigorously tested it). Well you should write some test to ensure validity of the function. So even after changes you can be sure it will still work. ...
• 11.5k

### Return first duplicate

Benchmark and slightly improved versions of some solutions. Congratulations, in the worst case (a = list(range(1, 10**5 + 1))) your original solution is about 2-4.5 ...
• 3,991
Accepted

### Implementation of a two-dimensional array in C

Enable more warnings, and use a memory checker: ...
• 75.7k