Questions tagged [array]

An array is an ordered data structure consisting of a collection of elements (values or variables), each identified by one (single dimensional array, or vector) or multiple indexes.

Filter by
Sorted by
Tagged with
141
votes
17answers
26k views

Searching an element in a sorted array

I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic ...
32
votes
8answers
17k views

is_numeric_array() is missing

I found that in PHP (or I probably can't find it) a proper is_numeric_array($array) function is missing. So I created one. The problem is that I don't think it's ...
30
votes
5answers
130k views

Modifying an array during a foreach loop [closed]

This question is always on my mind. I usually alter the array while looping through its keys, based on my gut feelings: ...
28
votes
8answers
44k views

Find all the missing number(s)

Recently I've attended an interview, where I faced the below question: Question: "There is an sorted array. You need to find all the missing numbers. Write the complete code, without using any ...
28
votes
3answers
78k views

Find all subsets of an int array whose sums equal a given target

I am trying to implement a function below: Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array. For example: ...
26
votes
7answers
7k views

Finding words of different lengths given six letters

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
24
votes
4answers
27k views

Reverse a String in Java

Here is the code for the CString class that I created. ...
24
votes
4answers
3k views

“ONCE”, “UPON”, “A”, “TIME”

I'm working on a small program to perform various tasks on text content, primarily at the word level. I wrote these methods as ways to help prepare the raw text file into something more malleable, ...
24
votes
3answers
135k views

Removing duplicates from an array quickly

...
24
votes
2answers
1k views

Mapping arbitrary Int values to a linear index space

This is part of a growing "primitive" tools collection here on github, and is the initial use-case for the IntArray for review here. Often, when processing data, you encounter unique values that you ...
23
votes
1answer
46k views

Snake game using PyGame

I wrote a simple Python snake game which is about 250 lines of code. Can someone give me some advice on how I can refactor/make it better? game.py ...
22
votes
6answers
7k views

Array whose values are the product of every other integer

I was asked the following interview question over the phone: Given an array of integers, produce an array whose values are the product of every other integer excluding the current index. ...
22
votes
3answers
3k views

Project Euler #1 in PHP

The code below works & as far as I can tell the result is correct. Would you please review and let me know what I could have done better? I tried to use variables as much as possible, that way it ...
22
votes
2answers
15k views

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

I am going to send a C++ array to a Python function as NumPy array and get back another NumPy array. After consulting with NumPy documentation and some other threads and tweaking the code, the code is ...
20
votes
4answers
18k views

Reverse words in a given String

I'd like this code to be improved Input string: codereview is awesome Output string: awesome is codereview My approach: ...
20
votes
4answers
2k views

Finding the sub-array with the maximum sum - my approach

This is the code I ended up with that implements the approach I described in a recent answer to another question about the same problem The basic idea here is to not loop through more things than ...
19
votes
8answers
48k views

Java function to read a CSV file

I have a java function that reads a csv and returns its content as a Vector<Vector<String>>. The function seems to work as I need it, but my gut ...
19
votes
1answer
6k views

VHDL mux in need of generics

I've built a multiplexer which takes 2 inputs: one array of std_logic_vector and one std_logic_vector to select the correct ...
17
votes
3answers
19k views

Index into array as if it is circular

The problem: Treat an array as if it is circular; out-of-bounds indices "wrap around" the array to become in-bounds. Current solution: ...
17
votes
5answers
4k views

Given a sequence of positive integers A and an integer T, return true if a continuous sequence of A sums up to exactly T

Here is the source of the question, and my solution is below. Did I determine the worst case correctly? If you find any input where it doesn't work, please let me know. ...
17
votes
4answers
1k views

Duplicate like a weapon, arrays like heaven

Challenge: Find the duplicated entry. Specifications: Your program should accept as its first argument a path to a filename. Each line in this file is one test case. Each line begins ...
17
votes
2answers
174 views

Fixed array with configurable bounds

A interesting challenge was brought to my attention: to "flip" an array. I thought it might be more convenient if its bounds were symmetrical, say [-N..N] instead ...
16
votes
2answers
79k views

Fastest way to iterate over Numpy array

I wrote a function to calculate the gamma coefficient of a clustering. The bottleneck is the comparison of values from dist_withing to ...
16
votes
6answers
37k views

Find common elements in a list of arrays

I need to find the common elements present in all the given arrays. All the arrays are present in another array. I have come up with this solution and it's working. I tried to remove the usage of <...
15
votes
4answers
670 views

Array alive/dead entity “refresh” algorithm

I have an Entity class that can be either dead or alive. I store entities contiguously in a resizable array. During an update()...
15
votes
2answers
2k views

Passing a 1D array (single column of a Range object) to .NET to manipulate it without loops and returning it back to VBA

Background Kind of a follow up - slightly related to my other SO question. I thought that if somehow I find a way in VBA to pass a single column to .NET and convert it to a native .NET type then I ...
14
votes
7answers
4k views

Determining whether two scrambled strings are equivalent

I was able to complete the problem doing it in this manner, but I would assume there has to be an easier way. Sample input: abc, cba Output: Both strings of characters are equivalent. Or ...
14
votes
4answers
4k views

Random name-picker with array

I created a program that picks a champion for a role on the game League of Legends using arrays and random numbers. Is there a quicker and more efficient way of doing this? There is one form that ...
14
votes
4answers
32k views

Writing a function to add or modify an existing object inside an array

I have an array of objects. Each object has two properties, id and quantity. I wrote a function to add an object to the array. ...
14
votes
2answers
933 views

Python ASCII-Art Table

As a part of a console utilities module, I created a function that takes a table in the form of an array of arrays, and generates an ASCII table with the given contents. I've also added the options ...
14
votes
2answers
17k views

Rotating array members

I had a situation when I needed to move all array elements in circular fashion. When I say this, I mean: 0 1 2 3 1 2 3 0 2 3 0 1 3 0 1 2 The array: ...
14
votes
4answers
402 views

Finding subtuple in larger collection?

I've got a flat array of < 1000 items and I need to find the indices of a smaller tuple/array within the array. The tuple to find can be a varying length (generally between 2 and 5 items, order is ...
13
votes
5answers
2k views

Zero-initializing large dynamically allocated arrays of double

Arrays of double can be zero-initialized with the { 0 } initializer, which works even on systems that have a binary ...
13
votes
10answers
21k views

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

Recently, I encountered this coding exercise on codility and the idea is A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by ...
13
votes
2answers
73k views

Extract numbers from a string-Javascript

Consider the following string in javascript: var string="border-radius:90px 20px 30px 40px"; I want to extract the 4 numbers from that string and store them in ...
13
votes
3answers
5k views

Partitioning an array based on a condition in Javascript

Using the array.filter function, I can efficiently pull out all elements that do or do not meet a condition: ...
12
votes
7answers
26k views

Given an array of integers, return all pairs that add up to 100

I was recently given this programming challenge on an interview, and I decided to use javascript to solve it. I did, but I'm not happy with my implementation. I can't help thinking there must be a ...
12
votes
4answers
4k views

Hackerrank “Sherlock Holmes” challenge

Watson gives to Sherlock an array: A1, A2, ⋯, AN. He also gives to Sherlock two other arrays: B1, B2, ⋯, BM and ...
12
votes
5answers
19k views

Removing n elements from array starting from index

This method removes n elements starting from a given index, from an array of a given type. If n is positive it removes elements forwards, if it is negative it removes them backwards (e.g. for an array ...
12
votes
3answers
6k views

Random permutation of int[] and ArrayList<Integer>

I wrote a simple program using int[] and ArrayList<Integer> which aims to get a random permutation output between 1 to 10, ...
12
votes
2answers
187 views

C++ data type to store and manipulate individual bits

The use cases I’m facing require me to have a C++ data type that can store and manipulate the state of somewhere between 400 and 500 bits. In addition it is necessary to be able to store that state ...
12
votes
2answers
125 views

Re-arranging an obfuscated address

I'm getting address (physical address, not digital) input that's obfuscated, and looks like the following: The plaintext version: ...
12
votes
1answer
1k views

Flatten an array of integers in Python

The goal is to flatten an array of nested arrays of integers. For example [1, [2], [3, [4]]] should return [1, 2, 3, 4] I'm ...
12
votes
1answer
17k views

Boolean expression parser

I was trying to write some of the Haskell list functions into Java, and I realized that one of the key strengths for many of the functions was the ability to pass in a boolean expression. I decided to ...
12
votes
1answer
3k views

Bubble Sort in C: Array vs Linked List

A few weeks ago I posted a code review of a linked list so I decided to implement bubble sort on my journey of learning C. Here is the referenced linked list library, though this isn't really what's ...
12
votes
2answers
28k views

in_array() in Go

This function's objective is very simple. It takes array and checks if val is a value inside of this array. It returns whether <...
12
votes
1answer
226 views

Array-like container for uints shorter than 8 bits

Follow-up: Array-like container for uints shorter than 8 bits (Rev 1) First of all: yes, I need this kind of array to generate data for a piece of hardware, and it must be stored and manipulated ...
11
votes
7answers
7k views

Generate random binary arrays

I am fairly new to C++ programming and I, so far, love it. I just have been trying to make sure what I am doing is fast, unique, readable, and efficient. Is there anyway I can improve this code, or is ...
11
votes
5answers
586 views

FiniteArrayQueue type with interface and unit tests

Doing some exercises on basic data structures, I learned about queues, and decided to roll my own to better understand how a basic queue can function. As the name indicates, this Queue is made from a ...
11
votes
5answers
2k views

Check if three arrays contains the same element

Do 3 arrays contain a common element, if they contain output it. For example: [1,2,3], [2,3], [2,4] -> answer = 2 ...