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

Following is an interview question asked by 'Amazon' to me. I still haven't come up with a optimized solution. Problem Statement: Given an unsorted array of integers n. Return 'true' if the addition

While iterating through a dataset, what's the best way to keep track of only the top 10 numbers so far, in sorted order? Solution...Ended up implementing Generic Min and Max Heaps...as sadly they are

I'm looking for an algorithm for intersection of two small, unsorted array in very specific condition. Type of array item is just integer or integer-like type. Significant amount of time (about 30~40

I have some data in an integer[] Native Array and I want to perform ArrayAppend / ArrayDeleteValue operations on it. I've noticed that this fails silently in Railo 3.3 because the native array types a

I naively thought I could use memset for this, but apparently memset is only for chars. Is there a memset-type thing that will work on an array of floats? Or is simple iteration the fastest way to cop

I am stuck up with two time complexities. To do a binary search with sorted array is O(logN). So to search an unsorted array we have to sort it first so that becomes O(NlogN). So then we can perform b

What is the fastest way to add string prefixes to array keys? Input $array = array( '1' => 'val1', '2' => 'val2', ); Needed output: $array = array( 'prefix1' => 'val1', 'prefix2' => 'val2

What is the fastest way of sorting this both alphabetically by country and numerically by date?: Array ( [JAPAN] => Array ( [2010-10-17] => 2 ) [CUBA] => Array ( [2010-10-16] => 9 ) [RUSSI

I'm not an expert of C++ and i'm looking to the fastest way to return a value from a function. For example, for passing a value as a function arguemnt the fastest way is a const reference: void foo(co

Thanks for your help i'm stuck on this problem. Let me explain it, i have this kind of array. First Array -------------- Array ( [2] => value7 [3] => value8 [4] => value9 [5] => value10

Previously answered questions here said that this was the fastest way: //nl is a NodeList var arr = Array.prototype.slice.call(nl); In benchmarking on my browser I have found that it is more than 3 t

I have a Cython function that takes a 2d nd.array (numpy array) of integers and returns a 1d numpy array whose length is the same as the input 2d array. import numpy as np cimport numpy as np np.impor

I wanted help regarding Java program to find out nearest match to any given integer in unsorted array of integers Can I please have suggestions about: * How to get start off with this? * Should i firs

As we know if n is not a perfect square, then sqrt(n) would not be an integer. Since I need only the integer part, I feel that calling sqrt(n) wouldn't be that fast, as it takes time to calculate the

I want to perform not operation to a block of continous elements of a bool array and then read back the complete array. I am using the following code to perform the operation. bool arr[100000]={0}; ci

Suppose you have several arrays of integers. What is a good way to find pairs of integers, not both from the same list, such that the difference between the first and second integer is 1? Naturally I

I'm trying to determine the fastest way to split a string in JavaScript into a multidimensional array with arbitary dimensions. Is there a way better than looping over the string and matching brackets

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

I have to create an array size of 10 and generate random numbers from 0 to 100 including 0 and excluding 100. When I write the code it keeps giving me an error of: Exception in thread main java.lan

Using scheme I have the need to use the following function. (All args are natural numbers [0, inf) ) (define safe-div (lambda (num denom safe) (if (zero? denom) safe (div num denom)))) However, this

I am new to PHP coding and here am looking for fastest way to do recursive search on all directories for an array of strings. I am doing this way $contents_list = array(xyz,abc,hello); // this l

Possible Duplicate: How do I quicky fill an array with a specific value? Is there a way to initialize an integer array with a single value like -1 without having to explicitly assign each item? Bas

I have an array like this and i m trying to search a value using this array: $arr = Array('Slovenly Mexico','Slovenly Europe','Greece'); I am using such kind of condition. please help if($a[storename

I have a javascript array like var test = [2,5,8,12,56]; and now I want to search the closest next value of 9. So the output is 12 in this case (and not 8!).

The title says it all. I see that I can create a binary search tree out of an unsorted array rather easily. If root is null, set 1st array value to the root current = root for each value in the arra

What is the fastest way to add a new item to an existing array? Dim arr As Integer() = {1, 2, 3} Dim newItem As Integer = 4 (I already know that when working with dynamic list of items you should rat

I need to know the fastest way possible, in PHP or Linux Command Shell, to reduce a text file that has more than 10 lines to only have the last 10 lines. I want to use 10 lines for this example. Thank

Is there a way to calculate the average distance of array elements from array average value, by only visiting each array element once? (I search for an algorithm) Example: Array : [ 1 , 5 , 4 , 9 ,

Say I have a two dimensional array: vectors[x][y], and the initial array structure looks like this: vectors = [ [0, 0, 0, 0, 0,], [0, 0, 0, 0, 0,], [0, 0, 0, 0, 0,], [0, 0, 0, 0, 0,], [0, 0, 0, 0, 0,]

Let's say we want to write a function in C that finds a specified target value in an unsorted array of ints. In general, this is simple and runs in O(n) time: int search(int *data, int len, int target

Using ActionScript 3, suppose I have an array of numbers, lets say: 1, 2, 3, 4, 5. Is there a way to easily search this array and return the index corresponding to an element that is >= 2.5 (which

I wanted to ask how lower_bound in cpp ( C++ ) behaves when it is applied on unsorted array. I mean when I ran the following program. #include <iostream> #include<vector> #include<algor

In a program I wrote, 20% of the time is being spent on finding out the minimum of 3 numbers in an inner loop, in this routine: static inline unsigned int min(unsigned int a, unsigned int b, unsigned

This was an interview question that I was asked to solve: Given an unsorted array, find out 2 numbers and their sum in the array. (That is, find three numbers in the array such that one is the sum of

list_ = [(1, 'a'), (2, 'b'), (3, 'c')] item1 = 1 item2 = 'c' #hypothetical: assert list_.index_by_first_value(item1) == 0 assert list_.index_by_second_value(item2) == 2 What would be the fastest way

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

Given a double variable named sizeValue and sizeValue contains something other than 0, what is the most efficient way to determine that sizeValue contains a value that is an integer? Currently i'm usi

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

I am able to find out the unique elements from a sorted array in my given piece of code. I need a solution for finding the unique elements by avoiding repetitive numbers i.e. from a Non sorted array o

We need to read and count different types of messages/run some statistics on a 10 GB text file, e.g a FIX engine log. We use Linux, 32-bit, 4 CPUs, Intel, coding in Perl but the language doesn't reall

Suppose I have an unsorted integer array a[] with length N. Now I want to find the kth smallest integer within a given interval a[i]-aj. Ex: I have an array a[10]={10,15,3,8,17,11,9,25,38,29}. Now I w

I have n numbers. n <= 1000000. Each number will be positive integer and less than 10^9. It is sure that there will be only one number will occur once, rest will occur twice or even number of times

I'm looking for a more elegant way to write the following code. I'm trying to grab the first (2) values of each line, the first as an integer and the second as a float, and place them into an array. P

I have a list of arrays of numbers. I am searching for the two arrays where my search number falls between the numbers positioned in index 0. Then return the number positioned in index 1 from the seco

Having an array of N sets, what is the fastest way to generate the inclusion graph of these sets?

I'm a new Ruby programmer performing an exercise to make a program that lets people play fish. I'm building a method that will search for books and handle them when it finds them. I have my player han

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 have two search loops for performing different operations, but I'm unhappy with how repetitive this looks. The first method used to remove an item is as follows: public void RemovePlayer(int theID){

Say we have the following string string data= /temp string; If we want to remove the first character / we can do by alot of ways such as : data.Remove(0,1); data.TrimStart('/'); data.Substring(1);

What is the fastest way to sort an array of whole integers bigger than 0 and less than 100000 in Python? But not using the built in functions like sort. Im looking at the possibility to combine 2 spor