I was asked this question recently in an interview :

What is the most efficient way to find a repeated number in a sorted array?

My answer was based on using a hash table with key as array element and number of repetitions in array as value; iterate the array and update hash table. In the end, hash table can be checked for elements with count > 1 ; those are the repeated elements.

Is there a better way to do this ?

Thanks.

Well, you can do this with space of `O(1)`

. Since its a sorted array, all you need to do it to subtract the current number with the next one. If the result is `0`

then you have a repeated number.

Check every element in the array (except the last one) with the next element, if they're equal then stop and exit: you've found a repeated element. It works because the array is sorted, it doesn't require any additional space and in the worst case (no repeated elements) it'll be O(n), because all of the array will have to be traversed.

-Sort the Array

-find the difference between adjacent indices values

-if the difference is 0 then there is a repeat and note the indices

Similar Questions

this is my question: I want to know which and how many times is a value repeated in a interval of a vector array, I know that many people will tell me that use hist, but I did it and the results isn

Hello I just have a simple question, why is the big O notation of a sorted array O(log N)? It will be a sorted array.

For example, to sort an array of complex numbers first by real part, then by magnitude of imaginary part, and then with negative imaginary parts first: def order(a): return a.real, abs(a.imag), sign(a

We have sorted array arr[]={2,4,5,7,8,12,16,18,20}. We need to find out pair of elements whose addition is 12, with complexity O(n). Could anyone help on it?

I have a dictionary that looks like this: example_dict = { 0: [(1,2),(3,4),(3,4),(4,5)], 1: [(1,2),(3,4),(5,6),(7,8)], 2: [(4,5),(7,8)]} I would like to get a sorted representation of this dictionary

suppose I have an array [1,2,3,1,5,2]. Here 1 and 2 are the repeated elements. I want to get a new array [1,2]. How do I do that in ruby ??

How do I get the array-members and the number of times they repeat (recurrences)? I currently have this script //COUNT VAL var curr = ''; var previous = ''; var arr = new Array(); var sorted = count.s

How can we find a repeated number in array in O(n) time and O(1) complexity? eg array 2,1,4,3,3,10 output is 3 EDIT: I tried in following way. i found that if no is oddly repeated then we can achieve

i cant figure out how to print no mode if there is more than one number that is repeated the same amount of times for ex. 5 5 6 6 7 6 9; since 5 and 6 are both repeated twice i want to print out no

What is the best way to remove from the array elements that are repeated. For example, from the array a = [4, 3, 3, 1, 6, 6] need to get a = [4, 1] My method works to too slowly with big amount of

Can I do a foreach on a function that takes an array object and sorts it. I know I can do a dependent observable, but I have an array of an array coming from the mapping plugin. foreach: sortList($arr

I am trying to locate the point of rotation in a sorted array through a modified binary search. Consider this array int values[9]={7, 8, 9, 1, 2, 3, 4, 5, 6}; The point of rotation here is at index =

I tried to sort using sorted dir =[A1,A2,A10,A3] sorted(dir) My expected array is [A1,A2,A3,A10] but actual result is [A1, A10, A2, A3] How to sort array by name in python ?

There is a sorted array which is of very large size. In it every element is repeated more than once except one element. How can I find the unrepeated element in O(log n) time complextiy.

I have to write a C++ code that finds the median and mode of an array. I'm told that it's much easier to find the mode of an array AFTER the numbers have been sorted. I sorted the function but still c

I'm having an issue sorting my random number array. What I'd like to do is make an if statement to make sure arr[0] is always greater than arr[1]. Why? Well, my program generates two random numbers

Given an infinite length sorted array having both positive and negative integers. Find an element in it. EDIT All the elements in the array are unique and the array in infinite in right direction. Th

What is the best optimal way of finding whether any element is repeated in a given array?

I have a sorted mutable array of a class called Topic. The topics represent a an array of Publications. I present the topics in a table, and periodically fetch new publications from a web service. Whe

I have a sorted array of integers: {1,2,4,4,5,8,12,15,15,23,54} I want to find how many numbers in that array fall between a range, say 4 and 15. {4,4,5,6,12,15,15} So, there are 7 items in the arr

Given a sorted array of integers, can we build a sorted array of the sums of all pairs in O(n^2)? A trivial solution would be to build the array of sums in O(n^2) and then to sort it in O(n^2 (log(n^2

Let's assume we have a sorted list: lst = [1,3,4,89,456,543] # a long one and what we'd like to do is to find the number of elements in a list which are smaller than, mx. Easy: n = len([x for x in ls

if an array of size n has only 3 values 0 ,1 and 2 (repeated any number of times) what is the best way to sort them. best indicates complexity. consider space and time complexity both

I've been trying to solve this problem in Excel for a while but I'm totally lost. I was able to solve the problem by using Python but I would need to implement the solution in Excel itself as well so

What would be the quickest way to insert values into the correct position in a numpy sorted array. For example I would like to insert ever value of binto a: a = [1,1,2,4,7,7,11,13,13,13,15,20,25,26,27

What is the complixety of turning a sorted array of size n to a legal 2-4 B tree? What would it be if the array wasn't sorted. I believe that the first answer should be O(logn) (As many splits that we

I have the following JavaScript array: var President = new Array(); President[0]=Thomas Jefferson; President[1]=Thomas Jefferson; President[2]=Thomas Jefferson; President[3]=Thomas Jefferson;

I have a problem with this code.... The project should show me repeated number in the input number. For example: $ ./a.out Enter a number: 9893746595 Repeated: 9 5 Here is the code: #include <stdi

How do I determine the index number of X in an array sorted by Y? I want to say something like The 6th record out of 26 where [array count] is 26 and 'F' is the record I'm looking for in an array co

Using the below for sorting a multidimensional array by screen name: $sortArray = array(); foreach($members as $member){ foreach($member as $key=>$value){ if(!isset($sortArray[$key])){ $sortArray[$

Given 3 sorted array. Find 3 elements, one from each array such that a+b=c. Can it be done less than O(n^3) time complexity? Please help me.

I used xsl:sort in apply-templates to sort the elements and I would like to have them also numbered, but if I try to use xsl:number, then it just gives the original position, not the one after sorting

I need to estimate if the array list is sorted (don't sort). When Strings are sorted, they are in alphabetical order. I try to use compareTo() method to determine which string comes first And return

I came across this problem - input - I am given two sorted arrays a1 and a2. I need to find the elements that are not present in the second array. I have two approaches 1) Hashtable - O(m+n) [use whe

Possible Duplicate: Unique entries in an array Let assume the array is sorted, how can I get number of counts for each unique items Example: $array = array (bye, bye, bye, hello, hello);

if an array is given in random order , you have to output the minimum number of swaps required to convert into cyclic sorted array. e.g. array given is 3 5 4 2 1 so the first swap will be 5<-->4

I have a set of images in a folder and I want to display images randomly in a condition for which user should be able to say the number of times within which image cannot be repeated. i.e if there are

I want to generate 10 random numbers from a population 1:1000, and the code that generate this number is repeated 10 times. I want the sampling to be without replacement such that the intersection bet

I have to develop an algorithm that will be able to sort a n-element list, where the first (n- root(n)) elements are sorted. Yes, this is a homework question. After some research on the internet and

I am actually trying to solve a problem where I have an array which is sorted but a few numbers are reversed . For example : 1 2 3 4 9 8 7 11 12 14 is the array. Now , my first thought was applying a

I know how to put an array in order, but in this case I just want to see if it is in order. An array of strings would be the easiest, I imagine, and answers on that front are appreciated, but an answe

let us consider following code >> B=xlsread('data_generations1','A1','g8:g301'); >> [pxx,f]=periodogram(B,[],[],100); >> [peaks,location]=findpeaks(pxx); >> [sorted_peaks,i]=so

Say I have an array of the following class sorted in ascending order by y: public class Obj { public int x; public int y; } How can I find the number of Obj items in the array that have y values with

I have an array, $items, which contains lot of informations: name, size, quantity, color, id... I'm using 'for' loops to sort $items values by color and save the sorted values in a new array, But the

Hi im just looking for help with this, i wrote a a quick piece of code to take in a character array which will then be run through a function which will delete any repeated characters i have a small b

I have an array like [1,1,1,2,4,6,3,3] and I would like to get the list of repeated elements, in this case [1,3]. I wrote this: my_array.select{|obj|my_array.count(obj)>1}.uniq But it is tragicall

I connect to MySQL database using PDO and fetch all the data. When I print out the array, the values are repeated. How to fix it? Thank you connect to DB: $db = new PDO(mysql:host=localhost:3306;dbna

What is an easy way in Java to sort the array of elements maintaining information about indexes where the elements were stored in the original array? Is there a built-in function? There are few ways I

In mercurial, how do you list commiters sorted by number of commits (commit count). Using git, you can do something like this : git shortlog -ns What is the equivalent command for mercurial ?

I am stuck at identifying repeated digits in a number and its count. The query, for example, should be able to identify DataColumn ----------- Result1(repeated digit)---------------Result 2 (count of