
-----------------------------------
ssikmace
Tue Apr 13, 2004 10:17 am

Removing duplicates from an array
-----------------------------------
How would I read a list of names from a file into an array and remove all duplicates.  The first line has the number of names in the file. Any help would be appreciated.

This is what I have so far:

%Reads a list of names and sorts them into array then outputs the sorted list
var streamin : int
var nameCount : int
var name : string

open : streamin, "Names.txt", get
assert streamin > 0

nameCount := 0
loop
    get : streamin, skip
    exit when eof (streamin)
    get : streamin, name
    nameCount := nameCount + 1
end loop

close (streamin)

open : streamin, "Names.txt", get
assert streamin > 0

var Names : array 1 .. nameCount of string
for i : 1 .. nameCount
    get : streamin, Names (i)
end for

close (streamin)

for i : 1 .. nameCount
    put Names (i)
end for

I dunno where to go from here. :?:

-----------------------------------
Delos
Tue Apr 13, 2004 10:53 am


-----------------------------------
Score them.

Create one array to hold the name/ID of each type of entry...eg.  "1's", "2's", "Cats", "Llama"...

Create a 2nd array to count the total number of that type and hold a reference to it...eg.
secondArray(2).tot := 3
secondArray(2).ref := "1;2;3"

Something to that effect...use records...makes your life easier.

-----------------------------------
Tony
Tue Apr 13, 2004 6:39 pm


-----------------------------------
eh... sord them, then check if any two adjesent elements are the same. If so, remove one of them and continue checking

-----------------------------------
Delos
Tue Apr 13, 2004 8:32 pm


-----------------------------------
Umm...yeah.  That's easier.

-----------------------------------
ssikmace
Wed Apr 14, 2004 10:01 am


-----------------------------------
Ok thx ill give it a try.

-----------------------------------
ssikmace
Wed Apr 14, 2004 10:37 am


-----------------------------------
Alright i got it to work. I had the program not add the name to the array if it was already in the array.

var streamin : int
var nameCount : int
var name : string

open : streamin, "Names.txt", get
assert streamin > 0

nameCount := 0
loop
    get : streamin, skip
    exit when eof (streamin)
    get : streamin, name
    nameCount := nameCount + 1
end loop

close (streamin)

open : streamin, "Names.txt", get
assert streamin > 0

var Names : array 1 .. nameCount of string (30)
var arrayCount := 1
for i : 1 .. nameCount
    get : streamin, Names (arrayCount) : *
    var found : boolean := false
    for j : 1 .. arrayCount - 1
        if Names (arrayCount) = Names (j) then
            found := true
            exit
        end if
    end for
    if not found then
        arrayCount := arrayCount + 1
    end if
end for

put "Here are the names without Duplicates"
for i : 1 .. arrayCount - 1
    put Names (i)
end for

-----------------------------------
ssikmace
Wed Apr 14, 2004 10:44 am


-----------------------------------
Heres a funky program

function yesno (word : string) : boolean
    var palin : boolean
    var mid : string
    var newword : string
    newword := ""
    for decreasing i : length (word) .. 1
        newword := newword + word (i)
    end for

    result word = newword
end yesno


var word : string

put "Please enter a word to determine if it is a palindrome."

get word

put yesno (word)

-----------------------------------
ssikmace
Wed Apr 14, 2004 10:46 am


-----------------------------------
var streamin, number : int
var filein := "names.txt"
var count : int

open : streamin, filein, get
assert streamin > 0


get : streamin, count

%reads a series of names and elimintes dupliiates%



var namelist : array 1 .. count of string (30)
var arraycount := 1
for i : 1 .. count
    get : streamin, namelist (i)
    var found : boolean := false
    for j : 1 .. arraycount - 1
        if namelist (arraycount) = namelist (j) then
            found := true
            exit
        end if
    end for
    if not found then
        arraycount := arraycount + 1
    end if
end for
close : streamin
%outputs list without duplicates%
put "here are the names without the duplicates"
for i : 1 .. arraycount - 1
    put namelist (i)
end for
