Hot answers tagged

38 votes
Accepted

Extract numbers from a string-Javascript

I used the following regexp to extract numbers from a string: var string = "border-radius:10px 20px 30px 40px"; var numbers = string.match(/\d+/g).map(Number);
user avatar
  • 947
32 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 ...
user avatar
27 votes
Accepted

Determining whether two scrambled strings are equivalent

This is where the Arrays utility class comes in handy. You can use it to sort the character arrays underlying both strings and then compare the sorted character ...
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" ...
user avatar
  • 16.4k
23 votes
Accepted

Duplicate like a weapon, arrays like heaven

@tim suggested a way to do this with less memory by sorting the array. There's another way to do it with less memory without needing to sort. Let's say that \$k\$ is the duplicate value in the array. ...
user avatar
  • 16.1k
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 ...
user avatar
  • 91.8k
22 votes
Accepted

Index into array as if it is circular

The arithmetic solution to this is relatively simple as we're talking modulo here: return i % N; This should produce the same results for your adjusted index. ...
user avatar
  • 25.1k
21 votes

Partitioning an array based on a condition in Javascript

Anything wrong with the naive ways? ...
user avatar
  • 356
20 votes
Accepted

Reverse words in a given String

There are several problems with your approach: It's strange to reverse the letters, reorder the words, and then reverse the letters again. It's inefficient and unnecessary. You could just reverse the ...
user avatar
  • 108k
20 votes

Project Euler #1 in PHP

You're using a brute force approach with a linear time complexity in the range and linear in the number of factors i.e. \$\mathcal{O}(nk)\$ where \$n\$ is the range of numbers (1000) and \$k\$ is the ...
user avatar
  • 16.4k
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 ...
user avatar
  • 10.1k
19 votes
Accepted

Removing duplicates from an array quickly

You can use the indexOf function to check if a value exists in an array. This would simplify your code greatly: ...
user avatar
  • 108k
19 votes
Accepted

Sum all numbers in a range

I don't know what you would use reduce() for to be honest. But you don't need to loop. We just have a simple arithmetic sequence with a straightforward formula: $$\...
user avatar
  • 18.3k
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 ...
user avatar
  • 2,722
18 votes
Accepted

in_array() in Go

Rather than tie yourself to only one type (string), you could use the reflect package as well as interfaces to make it somewhat type indifferent. The following is ...
user avatar
  • 2,794
18 votes
Accepted

VHDL mux in need of generics

There are 4 ways to a solve this: Solution 1: You can define a fixed width STD_LOGIC_VECTOR (SLV), which can be used to construct other vectors (let's call them STD_LOGIC_VECTOR_VECTORs (SLVV). <...
user avatar
  • 587
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 ...
user avatar
  • 14.6k
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 ...
user avatar
  • 13.8k
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 ...
user avatar
  • 9,195
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: ...
user avatar
  • 51.3k
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 ...
user avatar
16 votes

Project Euler #1 in PHP

Let's solve a simpler problem: Find the sum of all multiples of 3 below 1000. We have \begin{align} 3 + 6 + 9 + 12 + \cdots + 999 &= 3 (1 + 2 + 3 + 4 + \cdots + 333) \\ &= 3 \times \frac{333 ...
user avatar
  • 16.1k
16 votes
Accepted

Sending a C++ array to Python/NumPy and back

1. Review It's not recommended to use using namespace std; — the problem is that this imports all of the identifiers from std, ...
user avatar
  • 49.2k
16 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 ...
user avatar
  • 5,667
16 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 ...
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 ...
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 ...
user avatar
  • 10.1k
15 votes

Hackerrank "Sherlock Holmes" challenge

In 1801, a guy called Carl Friedrich Gauss studied problems where the number line wrapped around, called Modular Arithmetic. In his studies, he proved that: $$ (a \times b)\ \%\ n = [(a)\ \%\ n \...
user avatar
  • 96.8k
15 votes
Accepted

Project Euler #2 (Fibonacci Sequence)

My Assumptions about your requirements Here are a few points, assuming that you want performance and maintainability. Assumption: Since you did not put the generation of the sequence and the sum of ...
user avatar

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