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
23
votes
1answer
47k 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 ...
9
votes
2answers
10k views

How can I quickly find unique list items?

I use the loop-every-single-list-item approach to filter out unique elements in a given list, which tends to be very inefficient way as a list grow in size, or as function call frequency increases. ...
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 ...
7
votes
1answer
339 views

Calculating the number of rooms in a 2D house

I stumbled upon this problem on cses.fi (a problem set for the Finnish Olympiad in Informatics. This problem is in finnish so I'll translate it). I'll describe the problem, present the code and think ...
4
votes
4answers
500 views

Creating an array that loops out on the frontend - Don't Repeat Yourself?

Entire Code Here Don't repeat yourself I know of the "Don't repeat yourself". Still it's getting messy in some places. The code below contains three blocks of code. They are similar in some ways ...
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 ...
11
votes
4answers
9k views

Finding the first duplicate in an array

My code returns the correct answer the first duplicate is 3 (represented as int f) . I am struggling finding a more efficient way to find the first duplicate? My ...
10
votes
2answers
180 views

Associating degrees with notes of scales

I have a JS file that creates an object with notes from a music scale when given a key (aka tonic/ note). The code works and does what I want it to do. I need a critique on the way it's written, what ...
6
votes
2answers
168 views

Codility's Permutation Check in C#

I've made a few things in Java, and now I'm learning C#. The code passes the tests with 100% final score. I want to know what things can be improved in my code. Task description A non-empty ...
14
votes
2answers
997 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 ...
5
votes
3answers
5k views

Find the missing number in a array

I'm working on a personal project to practice my programming skills and I came to this problem. I tried to create a program that will find the missing number in a array. I'm looking forward to ...
5
votes
3answers
6k views

Split the array so that the sum of the numbers on one side of the split equals the sum of the numbers on the other side

I would really appreciate if you can help me to find a better way to do it. ...
4
votes
2answers
166 views

Find sum of number of times that each character of source occurs in the target

I wrote the following code in response to this question "find sum of number of times that each character of source occurs in the target", is there any better solution for it? Analysis ...
4
votes
3answers
1k views

(Re)Creating a tree from a flat collection (or “unflatten” a tree)

I need to create a tree from a flat collection. The order of elements is defined in a specification so it's rather impossible that it will ever change... but I want the whole algorithm to be ...
2
votes
2answers
620 views

Finding number of number pairs with given difference [closed]

pairs() is a function which returns the total number of combinations whose difference is k. ...
2
votes
1answer
50 views

Codility Cyclic rotation in C#

I'm learning C#, I programmed some things in java, and I would like to have a feedback on my C# code. The exercise is from codility. The goal is to rotate array ...
142
votes
17answers
27k 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 ...
24
votes
4answers
27k views

Reverse a String in Java

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

Removing duplicates from an array quickly

...
28
votes
8answers
45k 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 ...
18
votes
3answers
20k 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
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 ...
11
votes
3answers
2k views

Calculate min, max, mean and median out of array

Another C exercise: Write a function that takes an array of 'ints' as its input and finds the smallest and largest elements. It should also compute the median and mean. Use a ...
32
votes
5answers
133k 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: ...
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: ...
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 ...
11
votes
5answers
589 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 ...
9
votes
1answer
6k views

Implement numbering scheme like A,B,C… AA,AB,… AAA…, similar to converting a number to radix26

I want to implement numbering scheme like Microsoft Word uses for numbering. first one gets = A,next is B, next is C, .... then AA, then AB,....and so on. as shown below ...
9
votes
3answers
34k views

Implementing an ArrayList

I implemented ArrayList functionality in C as follows: ...
8
votes
4answers
15k views

Find the subarray with the max sum

In an interview I was asked to solve the following problem: find a subarray with max sum I have written a piece of code for the same. I need your help in reviewing this code. ...
7
votes
4answers
9k views

Reversing the lines of a file

I am trying to reverse the lines of a file by reading the file through an array. Is there a more efficient and quicker way of achieving this? ...
6
votes
1answer
4k views

Simple multi-dimensional Array class in C++11

The new version of the code can be reviewed in Simple multi-dimensional Array class in C++11 - follow-up. The following code implements a simple multi-dimensional array class (...
3
votes
2answers
1k views

Optimizing unboxed array operations in Haskell

Consider this simplified code which successively permutes array elements: ...
0
votes
2answers
2k views

Review implementation of stack by using array in C [closed]

This question has become long after many updates. Click here to go down. I have implemented stack using arrays in C. Please give me suggestions on how to improve it. The purpose of writing it is ...
12
votes
7answers
27k 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
1answer
229 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 ...
9
votes
2answers
244 views

Multi-dimensional container using operator[]

This is a late followup to a previous question of mine on same subject. Context: I want to have a tool that allows to use all the C++ goodies (algorithms, for loop on containers, etc.) on a ...
8
votes
5answers
26k views

Checking if an array is sorted

I've come up with two ways to check to see if array is sorted and return true if it is. ...
6
votes
3answers
323 views

Simplified implementation of std::vector in C++

I implemented a simplified version of a C++ std::vector/ArrayList learning from this link. From it, I tried to write an ...
4
votes
2answers
158 views

DoublingQueue in Java

Inspired by this CR question, I decided to create my own queue! If you guys see anything taboo or have any improvements, please let me know. Ultimately I want it to be feature rich and bug free, with ...
4
votes
2answers
2k views

Find median of two sorted arrays

Problem statement There are two sorted arrays nums1 and nums2 of size m and ...
2
votes
1answer
3k views

Optimise compare and match method using scripting.dictionary in VBA

In my previous post I was looking for a better solution to write a single column of a 2D array into a worksheet besides looping between code and worksheet and finally managed to achieve that. On ...
2
votes
1answer
1k views

Simple multi-dimensional Array class in C++11 - follow-up

Last week, I posted this question in order to get some comments and critiques on a simple implementation of a multidimensional array class -- hyper_array::array ...
10
votes
2answers
1k views

Using 2D arrays to make more efficient sequential storage

This code is part of a growing "primitive" tools collection here on github Use Case Consider: ...
9
votes
1answer
226 views

Earliest time frog can jump to the other side of a river in C#. Codility's task

I'm new to C#, I programmed a little in java in the past.I had the two previous exercises reviewed here (permCheck, cyclicRotation, with a 100% score, just like this one), and I'm applying what the ...
8
votes
2answers
370 views

Array-like container for uints shorter than 8 bits (Rev 1)

This is a follow-up to Array-like container for uints shorter than 8 bits In short: The PackedBitfieldArray should be a kind of container for packed (unsigned) ...
8
votes
3answers
20k views

Binary search for inserting in array

I have written a method that uses binary search to insert a value into an array. It is working, but i would like to get a second opinion to see if i didn't write too much code for it. aka doing same ...
6
votes
2answers
21k views

Method to convert String to int array

I was working on a suggestion for this question, but the method, I would've suggested the OP do something similar to, seems a bit convoluted. It's supposed to take a String input and return an array ...
6
votes
3answers
169 views

Tree abstraction

This code provides a Tree abstraction. I would like to finalize the code structure only in tree directory, before starting ...
6
votes
2answers
470 views

Weaving an array

In preparing this answer, one of the components was an algorithm to rearrange a sorted array in a particular way. To put it succinctly, here's the problem description: Given an array \$A\$ with \$...