Find the first non-repeated integer

Find the first non-repeated integer in this file .

In the sequence {1, 7, 3, 1, 4, 3}, the answer would be 7 . 7 and 4 are the only non-repeated integers; of these 7 appears first.

Click here to open the file.


The answer is 25.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

6 solutions

Thaddeus Abiy
May 9, 2014

Here is the number list from the text file:

1
2
3
4
5
6
7
8
Numbers=[117, 25, 199, 149, 183, 50, 114, 150, 149, 22, 13, 26, 86, 15, 151, 147, 16, 110, 16, 75, 65, 60, 114, 143,
         124, 89, 27, 147, 117, 146, 145, 67, 133, 143, 99, 172, 29, 64, 114, 9, 159, 85, 1, 120, 20, 4, 63, 54, 32, 90,
         36, 128, 137, 20, 150, 124, 55, 6, 42, 14, 93, 36, 1, 18, 185, 27, 89, 97, 96, 169, 54, 64, 36, 103, 196, 170, 112,
         191, 87, 179, 150, 50, 197, 167, 166, 190, 13, 116, 109, 35, 7, 61, 3, 111, 163, 11, 87, 113, 12, 174, 29, 162, 7, 10,
         190, 65, 97, 155, 152, 166, 199, 139, 76, 48, 178, 6, 164, 54, 11, 143, 134, 21, 198, 70, 107, 78, 5, 131, 62, 112, 155,
         122, 35, 94, 59, 142, 78, 108, 198, 156, 184, 98, 176, 36, 165, 154, 75, 80, 142, 26, 110, 75, 108, 134, 7, 57, 175, 76, 191,
         8, 123, 124, 53, 167, 76, 48, 81, 35, 132, 27, 160, 185, 82, 87, 145, 13, 154, 138, 3, 72, 75, 104, 71, 168, 15, 149, 61, 21,
         90, 103, 108, 39, 104, 151, 158, 75, 10, 142, 13, 130]

And here is the code that finds the required number. I think it is fairly easy to understand

1
2
3
4
for Number in Numbers:
    if Numbers.count(i)==1:
        print Number
        break

David Holcer
Apr 2, 2015
1
2
3
4
5
6
7
filee=open("cs_nonrepeatedint", "r")
lines = filee.read().split('\n')
i=0
for i in lines:
    if lines.count(i)==1:
        print i
        break

Tran Quoc Dat
Feb 14, 2015

In pascal:

uses crt; var a:array[1..200]of integer; f:text; const fi=’TEST.TXT’; procedure input; var i:byte; begin assign(f,fi); reset(f); for i:=1 to 200 do readln(f,a[i]); end; procedure output; var i,v:byte; begin for i:=1 to 199 do begin v:=i+1; while a[i]<>a[v] do begin v:=v+1; if v=200 then begin writeln(a[i]);exit;end; end; end; end; begin clrscr; input; output; readln; end. \boxed{\begin{array}{|} \text{uses crt;}\\ \text{var a:array[1..200]of integer;}\\ \text{f:text;}\\ \text{const fi='TEST.TXT';}\\ \text{procedure input;}\\ \text{var i:byte;}\\ \text{begin}\\ \text{assign(f,fi);}\\ \text{reset(f);}\\ \text{for i:=1 to 200 do readln(f,a[i]);}\\ \text{end;}\\ \text{procedure output;}\\ \text{var i,v:byte;}\\ \text{begin}\\ \text{for i:=1 to 199 do begin}\\ \text{v:=i+1;}\\ \text{while a[i]<>a[v] do begin}\\ \text{v:=v+1;}\\ \text{if v=200 then begin writeln(a[i]);exit;end;}\\ \text{end;}\\ \text{end;}\\ \text{end;}\\ \text{begin}\\ \text{clrscr;}\\ \text{input;}\\ \text{output;}\\ \text{readln;}\\ \text{end.}\end{array}}

Show the answer: 25.

CTRL+F on any browser and search number by number. It's pretty easy because the answer is the second appearance on the list...

Daniel Lim
Jun 29, 2014

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;

int numAppearance(vector <int> v, int n){
    int num = 0;
    for (int i = 0; i < v.size(); i++){
        if (v[i] == n)
            num++;
    }

    return num;
}

int nonrepeat(vector<int> v){
    for (int i = 0; i < v.size(); i++){
        if (numAppearance(v, v[i]) == 1)
            return v[i];
    }
}

int main(){

    freopen("answer.in", "r", stdin);
    freopen("answer.out", "w", stdout);

    int n = 200;

    vector <int> v(n);
    for (int i = 0; i < n; i++){
        cin >> v[i];
    }

    cout << nonrepeat(v);

    return 0;
}

A better way of doing it, IMO.

Bill Bell - 6 years, 1 month ago
Bill Bell
Apr 18, 2015

Although I'm not averse to using computer time rather than my own time for solving problems I would like to point out that using count on every element of the list uses computer time unnecessarily. Better to count the number of occurences of each item in the list first. There's a class in the standard Python collections module that makes this easy to do.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...