What can be the fastest way to search first repetitive value in an unsorted integer array of 10 numbers?

What should be the answer if the array has 10 million records?

If you want the smallest repeated number then

Sort the array with Radix sort in O(n) time

Loop through the sorted array and find the first repeat

If you want the first repeated number in whatever order the array is already in then

- Loop through the array adding the numbers to a Hash Set until you reach a number that you can't add to the Hash Set because it's it already exists.

The only way i can think of is using 2 array since you want `first repetitive value in an unsorted integer`

10 million ??? Not sure of memory implication

Memory footprint of splFixedArray is about 37% of a regular "array" of the same size. I was hoping for more, but that's also significant, and that's where you should expect to see difference, not in "performance

How big are PHP arrays (and values) really? (Hint: BIG!)

Example

```
$array = SplFixedArray::fromArray(array(1,2,4,6,4,2,7,7,3,3,1));
$list = array();
foreach ($array as $value ) {
if (in_array($value, $list)) {
echo $value;
break;
}
$list[] = $value;
}
```

Output

```
4
```

Here is benchmark code. It was too big to fit into comment, so I put it as separate answer.

```
<?php
$array = range(0, 10000);
$time_start = microtime(true);
$list = array();
foreach ( $array as $value ) {
if (in_array($value, $list)) {
echo $value;
break;
}
$list[] = $value;
}
printf("Using foreach loop:<br/>%0.10f<br/><br/>", microtime(true)-$time_start);
$time_start = microtime(true);
$list = array();
foreach ( new ArrayIterator($array) as $value ) {
if (in_array($value, $list)) {
echo $value;
break;
}
$list[] = $value;
}
printf("Using ArrayIterator:<br/>%0.10f<br/><br/>", microtime(true)-$time_start);
```

`foreach`

loop is faster than `ArrayIterator`

.

I tried with 10000 elements. Elements were generating with range function, which ensured I have 'the worst' input array, where all elements are different.

With 1 million records array generation took too long on my machine.

Similar Questions

In the below code I am getting the following error on line If (Not hash.Add(Numbers(Num))) Then Value of Type 'Integer' cannot be converted to 'System.Array'. What am I doing wrong? Module Module1 Sub

I am having a bit of a issue. I have an NSMutableDictionary with 10 NSMutableArrays in it. Each array has somewhere between 0-10 numbers which could each be any integer, e.g. 12 or 103. What I need to

Suppose I have a char buf[12]; which I know will always have a right justified unsigned number padded on the left with spaces. So, for example: _________329 (where the _ stands for space). The fastest

Given an unsorted integer array, and without making any assumptions on the numbers in the array: Is it possible to find two numbers whose difference is minimum in O(n) time? Edit: Difference between

Given an infinite positive integer array or say a stream of positive integers, find out the first five numbers whose sum is twenty. By reading the problem statement, it first seems to be 0-1 Knapsack

Consider a char array like this: 43 234 32 32 I want the last value that is 32 in integer form. The string size/length is not known. In the above example there are 4 numbers, but the size will vary.

I recently noticed trouble when using the array_search function in my code. I am searching the array $allcraftatts for the value sharp. I tried to isolate the problem by setting up a two line expe

What is the fastest way to compute absolute value of integer of long long int type in C++ ? Is it possible to do without if() statement ? I was trying conversion to unsigned and then to signed again,

This question already has an answer here: What is the best way to get the minimum or maximum value from an Array of numbers? 13 answers For example I have an array like below: int[] arrayOne =

I'm searching for the fastest way to know if a value exists in a list (a list with millions of values in it) and what its index is? I know all values in the list are unique like my example. My first m

I have an array of arrays for drawing a tilemap on the screen (basically an array of columns, each column is an array of tiles). I have tried to speed up the drawing process by not setting the array i

Lets say I've got an ordered NSArray of NSNumbers: 2, 4, 8, 15, 16, 20 // for simplicity let's treat it as array of int's instead of NSNumbers Now I need to find closest index to let's say value == 1

I have a 2d array and I want to know how do you set the first value so if my array was int array[a][b] = int[10][10]; How would you access index 'a' in a for loop? This is my simple code that I am w

I need the fastest and simple algorithm which finds the duplicate numbers in an array, also should be able to know the number of duplicates. Eg: if the array is {2,3,4,5,2,4,6,2,4,7,3,8,2} I should be

I want to regex search an integer value in MongoDB. Is this possible? I'm building a CRUD type interface that allows * for wildcards on the various fields. I'm trying to keep the UI consistent for a f

In looking for a way to efficiently determine the lowest positive integer that is not used as a value of a specific property in any object within an array of objects. In other words, I'm looking for a

What is the fastest way to write an array of unsigned short values into a file, and then what would be the fastest way to read these in another application? Also this would be on an apple machine runn

What is the fastest way of converting an array of floats into string in C#? If my array contains this { 0.1, 1.1, 1.0, 0.2 } Then I want each entry to converted to a string with value separated by a w

I have an array int x[] and a number. I like to do search on the array such that x[i] + x[i+1] = number. What is the most efficient and fastest way in Java?

Possible Duplicate: What is the best way to delete a large number of records in t-sql? What is the fastest way to delete massive numbers (billions) of records in SQL? I want to delete all the record

I'm currently creating a 24 puzzle. If I input move followed by 12 Then the array index that corresponds with the number will search if zero is adjacent to itself. If 0 is found, it will swap plac

When you do something like test in a where a is a list does python do a sequential search on the list or does it create a hash table representation to optimize the lookup? In the application I need

This question already has an answer here: numpy array initialization (fill with identical values) 5 answers I want to create a 3D array in Python, filled with -1. I tested these methods: import

I need to find out all the combinations of sum of any two numbers in an array. if it is equal then print them. The linear solution to this problem has O(N^2) complexity. I thought of sorting and then

What is the fastest and easiest way to get the last item of an array whether be indexed array , associative array or multi-dimensional array?

I have this array converted from xml of webservice. I have 500 items in this array. I want to search any value and return all found array items with key association ( similar to database select query

Lets say I have a schema like this: users: { friends: [{name: String, id: String}] } what is the fastest way to extract this object array into two separate arrays: var names = [String] var ids = [Str

I'm looking for the most efficient way to search a large array of a class. I have an array of tiles and need to find them by ID, the only way I've come up with is below although I'm sure there are far

a part of an assignment of mine is based on an array (its size is given by the user) which contains random numbers from 1 to 10^10. Then we have to find the k-th smaller number of the array. Here's wh

I have an unsorted array with n*n order. How to get the largest element from each row with complexity O(n logn).

I've been surfing around a little trying to find an efficient way to do this, but have gotten nowhere. I have an array of objects that looks like this: array[i].id = some number; array[i].name = some

The stdint.h header lacks an int_fastest_t and uint_fastest_t to correspond with the {,u}int_fastX_t types. For instances where the width of the integer type does not matter, how does one pick the int

I came across this document Binary Search Revisited where the authors have proved/explained that binary search can be used for unsorted arrays (lists) as well. I haven't grokked much of the document o

Consider an array of integers (assumed to be sorted); I would like to find the array index of the integer that is closest to a given integer in the fastest possible way. And the case where there are m

Pretty simple, and I thought I knew what I was doing, but apparently not. Anyways. I need to find the first element in a vector that is less than a specific value. Here is the code that I have been us

Given the array a = [1,1,12,3,5,8,13,21] I can slice off the first 3 elements like a[:3] giving [1,1,2]. What I want is to slice off up to the element of vlaue i (e.g. if i=8 I want [1,1,12,3,5,8] or

What's the fastest way to find the index of an item in a list or object in Javascript / AS3? I'm asking this for both because the syntax is similar for both these languages. Assuming: myArray = [one

In a stored procedure, I am generating an array of keys and want to compare it to an array of keys off a table. What is the fastest way to compare two arrays to see if both of them have at least one v

I am currently stumped while searching for an answer, but I have an array which contains information relating to a tournament, as seen below. I would like to search through this Array of arrays for s

Today I tried to find a funny and mysterious way to determine JavaScript's maximal integer value. One of the approaches was the following: ~(+!!![]) >>> (+!![]); which evaluates actually to

I'm trying to write my first Android app. It will take a number input by a user in an EditText field, convert it to an integer, then find the factors. I want to port this from a Java program that I wr

So I am just trying to figure out a way of putting randomly generated numbers into an array list. I am also trying to just put 10 numbers on a line for 10 lines for a total of 100 numbers printed out.

Suppose I have this array: $array = array('10', '20', '30.30', '40', '50'); Questions: What is the fastest/easiest way to remove the first item from the above array? What is the fastest/easiest way

I am trying to implement Breadth First Search for a maze using arrays. The problem I'm facing now is that I cant expand the paths at the same time. Something like this: ####### 123456# #3###7# #4###8#

What's the fastest/one-liner way to remove duplicates in an array of objects, based on a specific key:value, or a result returned from a method? For instance, I have 20 XML Element nodes that are all

What is the fastest way to select and reassign elements based on complex conditions in numpy, such as: # some 1-d array of floats myarray = np.array(myarray) # set any foo's or bar's from myarray to 0

This was given to me in a recent programming interview. I am given an unsorted array of integers with negative and positive values, and required to sort them but only for the positive values. I was wo

I have tried implementing depth first search in c# but I am not exactly sure how to do this the distributed computing way. If you guys can help me out in this i would be really grateful :) You can fin

I would like to know what is the best, fastest and easiest way to compare between 2-dimension arrays of integer. the length of arrays is the same. (one of the array's is temporary array)

I'm trying to write a function that is able to determine whether a string contains a real or an integer value. This is the simplest solution I could think of: int containsStringAnInt(char* strg){ for