top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find the first non-repeated element in an unsorted array?

+3 votes
458 views
How to find the first non-repeated element in an unsorted array?
posted Dec 15, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
static int FirstNonRepeated(IEnumerable<int> iArr)
{
    Dictionary<int, int> dict = new Dictionary<int,int>();
    int count = 0;

    int firstNumber = -1;

    foreach (int i in iArr)
    {           
        if (dict.ContainsKey(i))
        {
            dict.TryGetValue(i, out count);
            dict[i] = count++;              
        }
        else
        {
            dict.Add(i, 1);
        }

    }

    foreach (KeyValuePair<int, int> kp in dict)
    {
        if (kp.Value.Equals(1))
        {
            firstNumber = kp.Key;
            break;
        }
    }

    return firstNumber;
}
answer Dec 16, 2015 by Mohammed Hussain
0 votes

Algorithm :
1: scan the array from left to right.
1.a: Make count array to each element of given array.
2. scan count array from left to right.
2.a check current count array element is 1
2.b Print that element and stop
3: exit.

answer Dec 20, 2015 by Rajan Paswan
Similar Questions
0 votes

Given an unsorted array which has a number in the majority (a number appears more than 50% in the array), how to find that number?

+2 votes

consider an example :
if an array is: 2 -1 2 4 6 -5
then maximum sum of contiguous array is 13, from index 0 to index 4
constraints: complexity must not be greater than O(n).

+5 votes

Input: {5, 8, 5, 3, 3, 2, 7}
Output: {5, 8, 3, 2, 7, ?, ?}

Idea is that we should remove the duplicate number from the array and should return the array in the original form (obviously without duplicate).

Please help me with an efficient algorithm or C code.

...