Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
39 votes
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 ...
Igor Soloydenko's user avatar
27 votes
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" ...
Emily L.'s user avatar
  • 16.6k
25 votes

Partitioning an array based on a condition in Javascript

Anything wrong with the naive ways? ...
Qtax's user avatar
  • 396
23 votes
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 ...
Martin York's user avatar
  • 95.2k
20 votes

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 ...
FMc's user avatar
  • 12.7k
19 votes
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 ...
cbojar's user avatar
  • 2,732
18 votes
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 ...
Eric Lippert's user avatar
  • 14.9k
18 votes
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 ...
Rick Davin's user avatar
  • 6,350
18 votes

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 ...
dfhwze's user avatar
  • 13.9k
17 votes
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 ...
Tunaki's user avatar
  • 9,243
17 votes

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 ...
Oh My Goodness's user avatar
17 votes

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: ...
Reinderien's user avatar
  • 62.1k
17 votes
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 ...
Serge Ballesta's user avatar
16 votes
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 ...
Ilmari Karonen's user avatar
16 votes
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 ...
FMc's user avatar
  • 12.7k
15 votes

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: ...
morbusg's user avatar
  • 900
15 votes
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 ...
Roland Illig's user avatar
  • 21.3k
15 votes

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. ...
pacmaninbw's user avatar
  • 23.8k
15 votes

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 ...
slepic's user avatar
  • 5,477
14 votes
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, ...
Jakub Narębski's user avatar
14 votes
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 ...
Toby Speight's user avatar
  • 75.7k
14 votes
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 ...
Gerrit0's user avatar
  • 3,456
14 votes
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 ...
200_success's user avatar
14 votes

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 ...
vnp's user avatar
  • 56.5k
14 votes
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 <...
Edward's user avatar
  • 66.4k
14 votes

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. ...
Ludisposed's user avatar
  • 11.5k
14 votes

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 ...
superb rain's user avatar
  • 3,991
14 votes
Accepted

Implementation of a two-dimensional array in C

Enable more warnings, and use a memory checker: ...
Toby Speight's user avatar
  • 75.7k
13 votes

Extract numbers from a string-Javascript

The code will fail in below cases: Negative numbers Decimal numbers Units other than px(e.g. pt, ...
Tushar's user avatar
  • 3,002

Only top scored, non community-wiki answers of a minimum length are eligible